473,327 Members | 2,055 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,327 software developers and data experts.

Set of template class

Hi!

I'm trying to do something that is perhaps impossible to do.

I have two classes : Attribute and SetOfAttributes

Here is Attribute.h

template <typename T>
class Attribute
{
T _value;
};

I'm trying to do this :

class SetOfAttributes
{
std::vector<Attribute> _attributes;
}

int main(...)
{
SetOfAttributes set;
set._attributes.push_back(new ZAttribute<float>());
set._attributes.push_back(new ZAttribute<int>());
}

Which, of course, doesn't work. I can't find the correct syntax to do
it.

Any idea ? Thanks a lot!

Jun 23 '06 #1
7 1384
Fabien wrote:
Hi!

I'm trying to do something that is perhaps impossible to do.

I have two classes : Attribute and SetOfAttributes

Here is Attribute.h

template <typename T>
class Attribute
{
T _value;
};

I'm trying to do this :

class SetOfAttributes
{
std::vector<Attribute> _attributes;
}

int main(...)
{
SetOfAttributes set;
set._attributes.push_back(new ZAttribute<float>());
set._attributes.push_back(new ZAttribute<int>());
What's ZAttribute? Above, you said you have only two classes, Attribute and
SetOfAttributes.
}

Which, of course, doesn't work. I can't find the correct syntax to do
it.


Well, you'd have to make Attribute a non-templated polymorphic class and
derive ZAttribute as a template from it. Then you have to store pointers in
your vector.
Another thing you might be interested in is boost::any.

Jun 23 '06 #2
In article <1151095213.994987.92570
@u72g2000cwu.googlegroups.com>, fa***********@gmail.com
says...
Hi!

I'm trying to do something that is perhaps impossible to do.

I have two classes : Attribute and SetOfAttributes

Here is Attribute.h

template <typename T>
class Attribute
{
T _value;
};

I'm trying to do this :

class SetOfAttributes
{
std::vector<Attribute> _attributes;
}


Unless SetOfAttributes does more than this, you'd
probably be better off with:

typedef set::vector<Attribute> SetOfAttributes;

If you are going to add more to make it more capable,
then you've got a couple of choices. One is to make
_attributes public (e.g. by making SetOfAttributes a
struct instead of a class). Public data members are
generally regarded as a poor idea though...

Another is to add some (public) member functions to
SetOfAttributes that provide its operations, most of
which will probably just forward to _attributes.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 23 '06 #3
> What's ZAttribute? Above, you said you have only two classes, Attribute and
SetOfAttributes.
Sorry, I made a typo, Attribute means ZAttribute to me (I just wanted
to avoid my Z prefix).
Well, you'd have to make Attribute a non-templated polymorphic class and
derive ZAttribute as a template from it. Then you have to store pointers in
your vector.

That's what I did :

class Attribute
{
virtual ~Attribute(void)
{
}
};

template <typename T>
class AttributeT : public Attribute
{
private:
T _value;

public:
inline T getValue(void) const
{
return(_value);
}
};

class SetOfAttributes
{
std::vector<Attribute> _attributes;
};

It works, but I have to cast very often because AttributeT::getValue()
doesn't exist in Attribute. I also wanted to avoid heritage.

I was wondering if there was another solution that I did not know. I
wanted to avoid using Boost, but if it's the only solution to avoid
heritage... why not.

Thanks for your help!

Jun 23 '06 #4
> typedef set::vector<Attribute> SetOfAttributes;

Or like this ?

class SetOfAttributes
{
typedef std::vector<Attribute *> TSet;
typedef TSet::const_iterator TConstIterator;
typedef TSet::iterator TIterator;
};
If you are going to add more to make it more capable,
then you've got a couple of choices. One is to make
_attributes public (e.g. by making SetOfAttributes a
struct instead of a class). Public data members are
generally regarded as a poor idea though...
I'd prefer to use OOP and private data members, of course !
Another is to add some (public) member functions to
SetOfAttributes that provide its operations, most of
which will probably just forward to _attributes.


Well, okay, but how should I declare my std::vector to accept many
instances of Attribute with different templates?

Jun 23 '06 #5
Fabien wrote:
What's ZAttribute? Above, you said you have only two classes, Attribute
and SetOfAttributes.
Sorry, I made a typo, Attribute means ZAttribute to me (I just wanted
to avoid my Z prefix).
Well, you'd have to make Attribute a non-templated polymorphic class and
derive ZAttribute as a template from it. Then you have to store pointers
in your vector.

That's what I did :

class Attribute
{
virtual ~Attribute(void)
{
}
};

template <typename T>
class AttributeT : public Attribute
{
private:
T _value;

public:
inline T getValue(void) const
{
return(_value);
}
};

class SetOfAttributes
{
std::vector<Attribute> _attributes;
};

It works, but I have to cast very often because AttributeT::getValue()
doesn't exist in Attribute.


Well, there is no useful implementation in the base class, since the return
type depends on the dynamic type, so there is not much you can do about
that.
I also wanted to avoid heritage.
Well, polymorphism is the usual way to go if you want to store objects of
different types in one container.

I was wondering if there was another solution that I did not know. I
wanted to avoid using Boost, but if it's the only solution to avoid
heritage... why not.


You could have a look at boost::any and either use that or take it as an
inspiration for your own implementation.
Jun 23 '06 #6
In article <1151097822.149824.260330
@b68g2000cwa.googlegroups.com>, fa***********@gmail.com
says...
typedef set::vector<Attribute> SetOfAttributes;
Or like this ?

class SetOfAttributes
{
typedef std::vector<Attribute *> TSet;
typedef TSet::const_iterator TConstIterator;
typedef TSet::iterator TIterator;
};


What good is class SetOfAttributes if all you put into it
is typedefs? All you're defining is names, so if you
don't want them to be visible globally, you probably want
to put them into a namespace rather than a class.

[ ... ]
Well, okay, but how should I declare my std::vector to accept many
instances of Attribute with different templates?


Ah, now we get to the crux of the situation: you want to
create an array of heterogeneous objects. The answer is
that you shouldn't even attempt to do that -- no matter
how you try to do it, it's going to cause a problem. Your
_only_ real choice is to make everything in the vector
the same type. They might be (smart) pointers to objects
of different types, but the things you actually put into
the vector really all need to be the same type.

From there you have a couple of choices: you can store
pointers to completely unrelated types, with some
intelligence to get the original type of object back when
you need it, or you can store a pointer to a base class,
and (when necessary) have it point at instances of
derived objects.

An entirely different possibility is to simply store the
different types of objects separately from each other. If
you have a relatively small set of types to deal with,
this may be the cleanest method available.

From the sound of things, your design may simply need
more thought. I'm not sure whether you're better off
doing that on your own, or posting more about the problem
here, and having us help out, but it sounds like right
now your problem goes far beyond mere syntax.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 23 '06 #7
Thanks for your help! I know I can't store objects of different types
in the same container, but since it was a problem of template, I
thought there might be a hidden solution.

I'll go with polymorphism and come back to you if I have problems using
dynamic and const cast.

Thanks again.

Jun 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
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...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.