Map of Parent Type and calling child type copy constructor (try two)

B

Brandt Dusthimer

Sorry about the duplicate message. I tabbed and then hit space in the
code in my previous message, which I guess sends the message in my news
reader. Anyways...

I'm try to construct a map<int, Shape> so that I can keep a list of
Shapes. Here's a simplified version of my code so far:

class Shape
{
public:
enum Type {
CIRCLE,
SQUARE
};

Shape(Type myType, int myInt) :
myType_(myType), myInt_(myInt) {}

// other standard functions here

private:
Type myType;
int myInt_;
};

class Circle : public Shape
{
public:
Circle(int myInt, int someAddtionalInt) :
Shape(Shape::CIRCLE, myInt),
_someAdditionalInt(someAdditionalInt)
{ }

// other standard and shape generic functions here

private:
int someAdditionalInt;
};

class Square : public Shape
{
public:
Circle(int myInt, double someDouble) :
Shape(Shape::SQUARE, myInt),
_someDouble(someDouble)
{ }

// other standard functions here

private:
double someDouble;
};

int main(void)
{
std::map<int, Shape> myList;
Circle k(101, 102);
myList[100] = k;
}

This won't compile because map will want to access
operator=(const Shape& rhs) in the class Shape.

Now, I've tried writing operator= a variety of ways. Since I have the
enum Type, I can even tell what type the various Shapes are in the list.
However, I cannot figure out how (if there is a way) to reassign the
subclass part of the object's values from operator= in Shape, or call the
subclass's copy constructor instead of Shape's.

Anyways, I figure I'm probably going about this all wrong (and probably
skating on the fine line of poor class design.) Any
recommendations/insights?

Thanks,

Brandt Dusthimer
 
V

Victor Bazarov

Brandt said:
Sorry about the duplicate message. I tabbed and then hit space in the
code in my previous message, which I guess sends the message in my
news reader. Anyways...

I'm try to construct a map<int, Shape> so that I can keep a list of
Shapes. [..]

Don't. You can't do that. It won't do what you want, anyway...
What you need to do is to have

map<int,Shape*>

and then fill the map from 'new Circle', etc.

V
 
H

Howard

Brandt Dusthimer said:
Sorry about the duplicate message. I tabbed and then hit space in the
code in my previous message, which I guess sends the message in my news
reader. Anyways...

I'm try to construct a map<int, Shape> so that I can keep a list of
Shapes. Here's a simplified version of my code so far:

class Shape
{
public:
enum Type {
CIRCLE,
SQUARE
};

Shape(Type myType, int myInt) :
myType_(myType), myInt_(myInt) {}

// other standard functions here

private:
Type myType;
int myInt_;
};

class Circle : public Shape
{
public:
Circle(int myInt, int someAddtionalInt) :
Shape(Shape::CIRCLE, myInt),
_someAdditionalInt(someAdditionalInt)
{ }

// other standard and shape generic functions here

private:
int someAdditionalInt;
};

class Square : public Shape
{
public:
Circle(int myInt, double someDouble) :
Shape(Shape::SQUARE, myInt),
_someDouble(someDouble)
{ }


You'll want that constructor changed to Square instead of Circle, I assume.

And the heed Victor's advice, and use Shape* instead of Shape in the map.
Then you'll be able to use the objects polymorphically (i.e., by overriding
virtual methods in the subclasses).

-Howard
 

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

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top