473,396 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
3
by: Maheshkumar.R | last post by:
Hi groups, How i can command over the MDI CHIlD forms created dynamically at runtime from PARENT. Let say, i have generated 5 mdichild forms, but i want to work with child form1 from MDI...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
6
by: reandeau | last post by:
I'm building out a OO based app in PHP 5 but I'm getting a little confused on children contructing parents. I have a parent that looks like this: abstract Class State { protected $database;...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.