473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Generics and user defined types

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
2 1304

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wiktor Zychla | last post by:
I've read several documents about upcoming C# generics and I still cannot understand one thing. Would it be valid to write a code like this: class SomeClass { public void AMethod<T>(T a, T...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
4
by: Michael Sparks | last post by:
I started writing some code with the 2.0 beta compiler, and found out quickly that specialization is not supported with generics. For example, I want to do something like this: class number...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
1
by: JosAH | last post by:
Greetings, Introduction This week I'll write a bit about generics (those funny angular brackets). I need an example and decided to use sets and some of their operations. This weeks' article...
17
by: My interest | last post by:
How can I use * operator in C# generics? e.g. class foo<T_> { T_ v1, v2: public T_ squar() { return v1 * v2; /// wrong! }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.