473,407 Members | 2,315 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,407 software developers and data experts.

Issue with storing Parent/Child class in map

22
I've a class 'Parent'

I declare a map using
map<Parent, Parent> mymap;

Now I derive one child 'Child'
and I've to add it to this map, so I do a static_cast like this:
Child* key = new Child();
Child* val = new Child();
mymap.insert(pair<Parent, Parent>(static_cast<Parent&>(*key), static_cast<Parent&>(*val)));

Now mymap shows correct count, but it doesn't find anything.

I search by creating another Child object and sending it as key like this:
Child key;
map<Parent, Parent>::iterator iter = mymap.find(key);

I've overloaded < operator for Parent class.
Every child object has a char* in it.
There is no copy-constructor defined for Parent class.

I need to know if mymap is storing objects then why they are not searchable.
This is urgently required, please help.
Thanks
Oct 20 '07 #1
2 2117
sicarie
4,677 Expert Mod 4TB
jeet232-

I've renamed your thread title to actually describe your issue as outlined in our Posting Guidelines . (Did you know that properly titled threads actually get more traffic than those titled "URGENT help needed now!"?)
Oct 20 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Child* key = new Child();
Child* val = new Child();
mymap.insert(pair<Parent, Parent>(static_cast<Parent&>(*key), static_cast<Parent&>(*val)));
What are you doing??? This looks like nonsense.

key and val are child objects. They can never be Parent Objects. Your cast is terribly destructive. It tells the compiler to disregard that fact that you have a Child object and to make a copy of it as a Parent object thereby shopping off all the Child atrributes and member functions. Slicing is a big no-no.

Substituting a Child for a Parent only works in you use a Parent pointer or reference. That would mean your map has to contain Parent pointers or references.

Also, the map uses the first member of the pair as a key and the second member as the value. I can see where the Parent (pointer or reference) might be a value but I don't see how it can also be a key.

Considering the map has its own tree structure, what are you doing with your own Child/Parent??

And I'm not sure you even have a Child class since the Child of a Parent is also a Parent to another child. In effect, they are both Parents.
Oct 21 '07 #3

Sign in to post your reply or Sign up for a free account.

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...
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...
14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
6
by: jalkadir | last post by:
Let's say that I have this class: class Parent{ private: char* str; public: const char* getStr(){return str;} }; And then I create a child class class Child{ private: std::string str;...
1
by: Fleckman | last post by:
I have a situation where I need to add rows to tables with a Parent-Child relationship which presents a constraints violation when I reject the changes. Here is the scenario: I add a row to a...
4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
3
by: Eddie | last post by:
If FormMain = MDI parent, FormSub = Child parent, I execute FormSub from the menu like this way. FormSub^ sub = gcnew FormSub; sub->MdiParent = this; sub->Show(); This can generate child...
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.