Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 5th, 2006, 06:25 PM
MattWilson.6185@gmail.com
Guest
 
Posts: n/a
Default 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

  #2  
Old December 5th, 2006, 07:15 PM
Salt_Peter
Guest
 
Posts: n/a
Default Re: Wild Cards?


MattWilson.6185@gmail.com wrote:
Quote:
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.

  #3  
Old December 5th, 2006, 08:15 PM
MattWilson.6185@gmail.com
Guest
 
Posts: n/a
Default Re: Wild Cards?


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:
Quote:
MattWilson.6...@gmail.com wrote:
Quote:
Hi, I was wondering if there is any type of way to specify a generic
type for a list. I basicaly have
>
Quote:
class shape {
}
>
Quote:
class circle: public shape {
};
>
Quote:
class triangle: public shape {
};
>
Quote:
template <class T>
class red : public T {
}
>
Quote:
template <class T>
class blue : public T {
}
>
Quote:
template <class T>
class Blueish : public blue<T{
}
>
Quote:
now I have a class that manages all blueish objects
>
Quote:
class BlueishManager {
public:
void Add(Blueish<shape>* ent) ....
private:
list<Blueish<shape>* __list;
}
>
Quote:
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)
>
Quote:
list<Blueish<? shape>*__list; So I don't have to cast my objects to
add them?
>
Quote:
Right now I have to have a diffrent list for each object
>
Quote:
list<Blueish<triangle>*__listTriangle;
list<Blueish<circle>*__listCircle;
>
Quote:
but that might be a pain later if I add a new shape!
>
Quote:
Thanks
>
Quote:
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.
  #4  
Old December 5th, 2006, 08:35 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: Wild Cards?

MattWilson.6185@gmail.com wrote:
Quote:
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)
  #5  
Old December 5th, 2006, 10:35 PM
MattWilson.6185@gmail.com
Guest
 
Posts: n/a
Default Re: Wild Cards?

Alright thank you! So the best solution then is to restructure?

Matt

On Dec 5, 3:57 pm, Pete Becker <p...@versatilecoding.comwrote:
Quote:
MattWilson.6...@gmail.com wrote:
Quote:
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)
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles