Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2006

Tip: Looking for answers? Try searching our database.

Generics and user defined types

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Herby - 20 Mar 2006 11:27 GMT
I need to define my own types and arrays of these types.

These types are for the most part extensions of the built in types and
need to provide all the basic operations of arithmetic and relational.

If i was using C++ I would overload the operators against my types and
use STL Vector etc.
No problem.  No performance costs. No virtual functions, no casting
etc.

Performance is a big issue for us.

The solution hints of the use of templates. As these arrays must be
publicly available, implies i need to use generics.  But due to the
nature of how these work they cannot work directly with the basic
operators.

The following is what i have come up with:

interface class IType
{
public:

virtual IType^    Add( IType^ rhs );

};

-------------------------------------------------------------------

ref class CodeType : public IType
{
public:
    CodeType(void);
    ~CodeType(void);

    virtual    IType^    Add( IType^ rhs );

private:

System::String^        mCode;

};

==========================================

generic<typename T1>
where T1: IType
ref class Vector : public List<T1>
{
public:
    Vector(void);

Vector<T1>^    operator+( Vector<T1>% rhs );

};

Is this the best solution?
It seems like alot of virtual calls, indirection and casting to me.

If STL.NET was available, no doubt, i would simply use that.  So im
kind of trying to simulate what STL.NET would have provided if it was
available?

Is this solution right or wrong?
Can it be improved upon in terms of performance etc?

Thanks in advance.
Ben Voigt - 23 Mar 2006 16:23 GMT
>I need to define my own types and arrays of these types.
>
[quoted text clipped - 10 lines]
> The solution hints of the use of templates. As these arrays must be
> publicly available, implies i need to use generics.  But due to the

I'm partly guessing here, someone correct me if I'm wrong.

Templates can be used in publicly available classes.  But your clients will
only be able to use the template instances, not use your template.

i.e.
template<class T>
ref class Vector
{
//...
};

public ref class Outer
{
   public:
       Vector<String^> stringVec;
};

should be allowed, and your clients can use stringVec.  However they can't
declare a Vector<Object>, because template expansion is done at compile-time
vs generics which are JITed.

> nature of how these work they cannot work directly with the basic
> operators.
[quoted text clipped - 49 lines]
>
> Thanks in advance.
Herby - 23 Mar 2006 17:13 GMT
Cheers Ben,

But consider  class Outer is more generic in that it could be point to
either of

Vector<String^>
Vector<Decimal^>

This is determined at runtime.
So it kind of implies can only have

Vector<Object^> as the member variable for the array.
or my own specialized derived type like Type.

Outer--------------------------Vector -------------------------------
Type
                                  1      |
0:M           |
                                      VectorString - - - - - - - - - -
- - StringType

Now consider a method on the Vector class called Add:

void            Vector::Add( Vector^ rhs )
{

            for( int c=0; c<Count; c++ )
                     this[c]->Add( rhs[c] );

}

Type would then have a virtual method called Add as well

StringType::Add( Type^ rhs )
{

}

The rhs side parameter here could be a DecimalType so i will need to
apply a cast to rhs side too now.

}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.