Wild Cards?

  • Thread starter MattWilson.6185
  • Start date
M

MattWilson.6185

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<triangle> to a Blueish<shape> causes
all the functions of the Blueish<triange> to 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
 
S

Salt_Peter

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<triangle> to a Blueish<shape> causes
all the functions of the Blueish<triange> to 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.
 
M

MattWilson.6185

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<Mesh> is 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<Mesh> Mobile<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
 
P

Pete Becker

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)
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top