473,807 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Issue with storing Parent/Child class in map

22 New Member
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(pa ir<Parent, Parent>(static_ cast<Parent&>(* key), static_cast<Par ent&>(*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>::iterat or 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 2144
sicarie
4,677 Recognized Expert Moderator Specialist
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 Recognized Expert Moderator Expert
Child* key = new Child();
Child* val = new Child();
mymap.insert(pa ir<Parent, Parent>(static_ cast<Parent&>(* key), static_cast<Par ent&>(*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
3835
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 create a child object the constructor for the parent object is called. That works fine. But now I have the problem that I want to add an already existing Parent object to create a new Child object. How can this be done?
16
2675
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 option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
14
5638
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 storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
6
10977
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; public: std::string& getStr(){return str;}
1
8591
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 Parent table which then causes a row to be added to the Child table referencing the parent row. The Child table rows can be modified by multiple processes so I need to manage row additionals separate from row modifications in terms of how they are...
4
4573
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 adding to the child collection, eg: parent.Children.Add(child); child.Parent = parent;
7
2041
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'. How should I declare classes so that all three functions are executed when child class is called? Class Parent public function ONE 'code end fuction
3
2697
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 form sub in FormMain. In th FormMain, there is status bar label named statusBar; I'd like to add code in the sub form that can access statusBar of parent
10
19628
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 Class Parent '*** HOW TO DECLARE IT *** Dim Age As String = "1/1/2000"
0
9599
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10624
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.