Generics and user defined types

H

Herby

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.
 
B

Ben Voigt

Herby said:
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
 
H

Herby

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.






}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top