By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,917 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,917 IT Pros & Developers. It's quick & easy.

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

P: n/a
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, Shapeso 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, ShapemyList;
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

--
Posted via a free Usenet account from http://www.teranews.com

Jul 12 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Brandt Dusthimer wrote:
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, Shapeso 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 12 '06 #2

P: n/a

"Brandt Dusthimer" <du******@u.netwrote in message
news:pa****************************@u.net...
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, Shapeso 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

Jul 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.