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
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