473,407 Members | 2,326 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.

How to tackle this nasty map

22
Can someone pls tell me what is the exact way of stroing user-defined objects in stl map??

I've a class: Parent
I derive two classes Child1 & Child2 from it

now I declare my map as: map<Parent*, Parent*> mymap;

what i need is that while storing child1/child2, i'll static_cast both to Parent & store in.
and at find time, do the reverse static_cast & get original object.

PLS help me solving it.
I doubt if I can store pointers in map or not.
But which operators to overload in Parent class or whereelse?

I've searched net but got more confused. please help.
Oct 19 '07 #1
4 1091
JosAH
11,448 Expert 8TB
Have a look at the 'double dispatch' or the 'visitor' pattern. Visiting a parent pointer
reveils which one you're dealing with: a child1 or child2 which, I believe is exaclty
what you want.

kind regards,

Jos
Oct 19 '07 #2
jeet232
22
Hi Jos,
thanks for the reply but that doesn't solve my problem,

I'm looking at basic level that how to use a map with user-defined objects

Regards
Jeet
Oct 19 '07 #3
JosAH
11,448 Expert 8TB
Hi Jos,
thanks for the reply but that doesn't solve my problem,

I'm looking at basic level that how to use a map with user-defined objects

Regards
Jeet
You may not realize it (yet?) but that simple Visitor pattern *does* solve your
problem in a neat way. Have a look in the C/C++ Articles section: WFC wrote a
nice article about it; or ask our friend Google ...

kind regards,

Jos
Oct 19 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
[jeet232]
what i need is that while storing child1/child2, i'll static_cast both to Parent & store in.
and at find time, do the reverse static_cast & get original object.
[/quote]

Generally, if you cast in C++ as part of your normal coding it means a) you are calling a relic C function that maybe has void* arguments, or b) your C++ design is screwed up. That is, if you have to tell a C++ compiler what the type is, then you have a problem.

C++ casts are intended to get you out of bad situations and are not to be just one of the daily activities.
Oct 19 '07 #5

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

Similar topics

1
by: Derek Fountain | last post by:
I've received some input from the user's browser, checked it for unpleasant stuff, and determined that it contains characters I'm not happy with. I'd like to store it or email it to an...
48
by: Zenobia | last post by:
Recently I was editing a document in GoLive 6. I like GoLive because it has some nice features such as: * rewrite source code * check syntax * global search & replace (through several files at...
7
by: Marek Mänd | last post by:
When using <TABLE rules="all"> Mozilla 1.6 and previous versions draw a nasty interiour border, which doesnt go away though I have on TD's css border:none set. Is it something wrong with my...
0
by: Jonas Smithson | last post by:
<div style="position: relative;> <!-- I'm the parent --> blah blah blah <div style="position: absolute; bottom: 0;> <!-- I'm the child --> blah blah blah </div> </div> The bottom of the...
8
by: Mark Johnson | last post by:
Using: VS 2003 NET C# for Framework and Framework Compact Trying : Moving a Card (Bitmap) as in Solitare (PC + WinCe) Version on OnMouseMove Problem : The affected drawing Area by Invalidate (or...
19
by: Jerry | last post by:
I managed to narrow this down to a very simple expression. try this: private void Bug() { bool b = false; Test(3, (b || b) && b && !b); } private void Works() {
1
by: Dejan Vesic | last post by:
I found nasty "documentation" bug; ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemglobalizationcultureinfoclasstopic.htm claims that proper culture info name for Serbian (Cyrillic) - Serbia...
1
by: james | last post by:
I am creating an connection string at runtime using decryption (crypto libraries). i have a very unpridictable error while assinging the generated connection string to the connection property. ...
4
by: Mario | last post by:
Hi there, I'm trying to do an easy thing... but no luck so far... I have a form with a textbox, and I want the following behaviour on mouse click event: -if no text is selected, select all...
6
by: S Wheeler | last post by:
I have a REALLY nasty problem and could use some expert help. I have two mixed mode apps. Each uses a mixed mode DLL that exports a couple of classes. There is no dll main as it is not needed....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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.