By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

Generics and user defined types

P: n/a
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.

Mar 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Herby" <pr********@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
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
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.

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.

Mar 23 '06 #2

P: n/a
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.


}

Mar 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.