469,575 Members | 1,618 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Wild Cards?

Hi, I was wondering if there is any type of way to specify a generic
type for a list. I basicaly have

class shape {
}

class circle: public shape {
};

class triangle: public shape {
};

template <class T>
class red : public T {
}

template <class T>
class blue : public T {
}

template <class T>
class Blueish : public blue<T{
}

now I have a class that manages all blueish objects

class BlueishManager {
public:
void Add(Blueish<shape>* ent) ....
private:
list<Blueish<shape>* __list;
}

The problem is casting a Blueish<triangleto a Blueish<shapecauses
all the functions of the Blueish<triangeto be overwritten. The shape
part still will call all of the Triangle data, but I create a new
Blueish class. Is there anyway I can make a functions like in java
(I'm no java programer but I have seen it)

list<Blueish<? shape>*__list; So I don't have to cast my objects to
add them?

Right now I have to have a diffrent list for each object

list<Blueish<triangle>*__listTriangle;
list<Blueish<circle>*__listCircle;

but that might be a pain later if I add a new shape!

Thanks

Matt

Dec 5 '06 #1
4 4960

Ma*************@gmail.com wrote:
Hi, I was wondering if there is any type of way to specify a generic
type for a list. I basicaly have

class shape {
}

class circle: public shape {
};

class triangle: public shape {
};

template <class T>
class red : public T {
}

template <class T>
class blue : public T {
}

template <class T>
class Blueish : public blue<T{
}

now I have a class that manages all blueish objects

class BlueishManager {
public:
void Add(Blueish<shape>* ent) ....
private:
list<Blueish<shape>* __list;
}

The problem is casting a Blueish<triangleto a Blueish<shapecauses
all the functions of the Blueish<triangeto be overwritten. The shape
part still will call all of the Triangle data, but I create a new
Blueish class. Is there anyway I can make a functions like in java
(I'm no java programer but I have seen it)

list<Blueish<? shape>*__list; So I don't have to cast my objects to
add them?

Right now I have to have a diffrent list for each object

list<Blueish<triangle>*__listTriangle;
list<Blueish<circle>*__listCircle;

but that might be a pain later if I add a new shape!

Thanks

Matt
Your inheritance hierachy is a little strange. I see more in common
between a red_triangle and a blue_triangle than between a red_rectangle
and a red_circle.
Anyway, You could argue that all shapes have_a color, including a
default of Gray or B&W.
Note that is not an is_a relationship. A shape is *not* a color.

class Color { ... };
class Gray : public Color { ... };
class Blue : public Color { ... }; // etc

template < typename C = Gray >
class shape
{
C color; // shapes -have- color
public:
shape() : color() { } // default ctor for target color invoked
virtual ~shape() = 0;
virtual double area() const = 0;
};

template< typename C = Gray >
class triangle : public: shape< C >
{
double height;
double width;
public:
triangle() : height(0.0), width(0.0) { }
...
double area() const { return 0.5 * height * width; }
};

#include <list>

int main()
{
triangle< /*Gray*/ gray_tri;
std::list< shape< /*Gray*/ >* graylist;
graylist.push_back( &gray_tri );

std::list < shape< Blue >* bluelist;
// and so on
}

What you may not want to sacrifice, is those properties that make a
traingle a triangle. Example: how you calculate its area() vs how a
rectangle or circle calculate area().
Color is nothing more than part of the object's composition.

Dec 5 '06 #2

Thank you so much for the quick responce! That is a great sugestion,
but it was just an example. I wanted to more get down to the wild card
equivelent in C++, I will look at my code again and see if I can
restructure it, but here is how it rests right now. It's a computer
graphics program that allows you to create diffrent types of tangable
entities, Mobile, Fixed, Active, and asigning them modifiers, a mesh or
camera

class Entity {}

class MobileEntity {}

class FixedEntity {}

template <class T>
class Mobile: public MobileEntity, public T {}

template <class T>
class Fixed:public FixedEntity, public T {}

template <clas T>
class Active: public Mobile<T{}

class Modifier {}

class Mesh : public Modifier {}

class Camera: public Modifier {}
The diffrence between mobile and fixed is the functions they have for
moving around the 3d world.
Active give the function addKey() and then recieves key presses which
can move the mobile object

Now to make an object i say

Mobile<Mesh>* actMesh= new Mobile<Mesh>;

Now Mobile<Meshis a mobile entity, that is a mesh

I don't know if it would make sence to say it is a MobileEntity that
HAS a Mesh because of how the functions are accessed, if I want to
dynamicaly call mesh functions for a list of all types of Mobile,
Fixed, and Active entities i can cast it as a mesh and call thoes,
instead of having to have something inside Mobile Active, and Fixed
classes

((Mesh*)actMesh)->getBoudningSphere();
I will have to think of it a little more I'm sure it is quite possible
to change it to the format you sugested, and if this format still seems
strange to you I will look more into it! But using it so far has
seemed intuitive, the only problem comes when I can have a list of a
Entities with diffrent modifiers I.E. Mobile<MeshMobile<Camera>, I
have to store a diffrent list for each one.

but again, is there any way to do wildcards in c++? Or is it nessisary
to figure out a diffrent structure?

list<Active<? Modifier>*__list

On Dec 5, 2:37 pm, "Salt_Peter" <pj_h...@yahoo.comwrote:
MattWilson.6...@gmail.com wrote:
Hi, I was wondering if there is any type of way to specify a generic
type for a list. I basicaly have
class shape {
}
class circle: public shape {
};
class triangle: public shape {
};
template <class T>
class red : public T {
}
template <class T>
class blue : public T {
}
template <class T>
class Blueish : public blue<T{
}
now I have a class that manages all blueish objects
class BlueishManager {
public:
void Add(Blueish<shape>* ent) ....
private:
list<Blueish<shape>* __list;
}
The problem is casting a Blueish<triangleto a Blueish<shapecauses
all the functions of the Blueish<triangeto be overwritten. The shape
part still will call all of the Triangle data, but I create a new
Blueish class. Is there anyway I can make a functions like in java
(I'm no java programer but I have seen it)
list<Blueish<? shape>*__list; So I don't have to cast my objects to
add them?
Right now I have to have a diffrent list for each object
list<Blueish<triangle>*__listTriangle;
list<Blueish<circle>*__listCircle;
but that might be a pain later if I add a new shape!
Thanks
MattYour inheritance hierachy is a little strange. I see more in common
between a red_triangle and a blue_triangle than between a red_rectangle
and a red_circle.
Anyway, You could argue that all shapes have_a color, including a
default of Gray or B&W.
Note that is not an is_a relationship. A shape is *not* a color.

class Color { ... };
class Gray : public Color { ... };
class Blue : public Color { ... }; // etc

template < typename C = Gray >
class shape
{
C color; // shapes -have- color
public:
shape() : color() { } // default ctor for target color invoked
virtual ~shape() = 0;
virtual double area() const = 0;

};template< typename C = Gray >
class triangle : public: shape< C >
{
double height;
double width;
public:
triangle() : height(0.0), width(0.0) { }
...
double area() const { return 0.5 * height * width; }

};#include <list>

int main()
{
triangle< /*Gray*/ gray_tri;
std::list< shape< /*Gray*/ >* graylist;
graylist.push_back( &gray_tri );

std::list < shape< Blue >* bluelist;
// and so on

}What you may not want to sacrifice, is those properties that make a
traingle a triangle. Example: how you calculate its area() vs how a
rectangle or circle calculate area().
Color is nothing more than part of the object's composition.
Dec 5 '06 #3
Ma*************@gmail.com wrote:
Thank you so much for the quick responce! That is a great sugestion,
but it was just an example. I wanted to more get down to the wild card
equivelent in C++, I will look at my code again and see if I can
restructure it, but here is how it rests right now.
Forget wildcards. Java's generics have almost nothing in common with C++
templates other than notation.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 5 '06 #4
Alright thank you! So the best solution then is to restructure?

Matt

On Dec 5, 3:57 pm, Pete Becker <p...@versatilecoding.comwrote:
MattWilson.6...@gmail.com wrote:
Thank you so much for the quick responce! That is a great sugestion,
but it was just an example. I wanted to more get down to the wild card
equivelent in C++, I will look at my code again and see if I can
restructure it, but here is how it rests right now.Forget wildcards. Java's generics have almost nothing in common with C++
templates other than notation.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Steve T | last post: by
2 posts views Thread by kelvSYC | last post: by
10 posts views Thread by Arun Nair | last post: by
7 posts views Thread by W. eWatson | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.