473,383 Members | 1,803 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,383 software developers and data experts.

std::map problem with inherited classes

Hi, everybody

I'm working with STL container map to store an element. Everything
happens well when I've an simple class (whit no inheritance). But if
using inherited classes I got some estrangs behaviors

Ex.
Class1 {
public:
int value;
Class1(int value) {
this->value = value;
}
Class1();
}

Class2 : public Class1 {
public:
int value2;
Class2();
Class2(int value1, int value2) : Class1(value1) { this->value2=value2
}
}

std::map<int, Class2> map_Class;

Class2 tmp = Class2(1, 9);

map_Class[0] = tmp;

tmp = map_Class[0]; // wrong value to base class attribute.

tmp.value2 == 9
tmp.value == ??? /wrong value;

Mar 21 '06 #1
6 2380
F. Meyer wrote:
Hi, everybody

I'm working with STL container map to store an element. Everything
happens well when I've an simple class (whit no inheritance). But if
using inherited classes I got some estrangs behaviors

Ex.
Class1 {
public:
int value;
Class1(int value) {
this->value = value;
That is obfuscating syntax. Why not:
Class1 (int v) : value(v) {}
}
Class1();
}

Class2 : public Class1 {
public:
int value2;
Class2();
Class2(int value1, int value2) : Class1(value1) { this->value2=value2
Ditto.
}
}

std::map<int, Class2> map_Class;

Class2 tmp = Class2(1, 9);

map_Class[0] = tmp;

tmp = map_Class[0]; // wrong value to base class attribute.
What is tmp?
tmp.value2 == 9
tmp.value == ??? /wrong value;


The FAQ may help:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Best regards,

Tom

Mar 21 '06 #2
>>Class2(int value1, int value2) : Class1(value1) { this->value2=value2

The above line is passing the both arguments to Class1
I susspect what you really want is the following:
Class2(int value1_, int value2_) : Class1(value1_) { value2=value2_;}

OT: FYI: Sorry if this ends up being a top post. I'm trying googles
webased message post to see if they fixed it.

Mar 21 '06 #3

Axter wrote:
Class2(int value1, int value2) : Class1(value1) { this->value2=value2

The above line is passing the both arguments to Class1


It is? Why do you say that?
I susspect what you really want is the following:
Class2(int value1_, int value2_) : Class1(value1_) { value2=value2_;}


Or better yet:

Class2 (int value1_, int value2_) : Class1(value1_), value2(value2_) {}

Best regards,

Tom

Mar 21 '06 #4
I've an awanser by myself, I must implement the operator =, for Class1
and Class2.

Tks.

Ps. Thomas, I know how to use a constructor, I don't ask for your teach
;)

Mar 21 '06 #5

F. Meyer wrote:
I've an awanser by myself, I must implement the operator =, for Class1
and Class2.
The classes you posted do not require you to implement operator=() -
the compiler-generated versions should do just fine.

Tks.

Ps. Thomas, I know how to use a constructor, I don't ask for your teach
;)


You're welcome - happy to help.

Best regards,

Tom

Mar 21 '06 #6

"F. Meyer" <fm*******@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Hi, everybody

I'm working with STL container map to store an element. Everything
happens well when I've an simple class (whit no inheritance). But if
using inherited classes I got some estrangs behaviors

Ex.
Class1 {
public:
int value;
Class1(int value) {
this->value = value;
}
Class1();
}

Class2 : public Class1 {
public:
int value2;
Class2();
Class2(int value1, int value2) : Class1(value1) { this->value2=value2
}
}

It's generally preferable to use different names for the parameters than the
member names themselves. That will also allow you to put value and value2
in their respective initializer lists, instead of in the function bodies.


std::map<int, Class2> map_Class;

Class2 tmp = Class2(1, 9);
Why not just:

Class2 tmp(1,9)?

map_Class[0] = tmp;

tmp = map_Class[0]; // wrong value to base class attribute.

tmp.value2 == 9
tmp.value == ??? /wrong value;


Nothing in your posted code indicates such a problem. I suspect you've left
something important out of the code, especially given your comment elsewhere
that you needed to add operator=. (Note: if you DO need operator=, then you
probably also need a destructor and a copy constructor. Look up the "Rule
of three".)

-Howard

Mar 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Christopher Benson-Manica | last post by:
If you liked the functionality of std::map, but found that you couldn't trust your implementation to handle nonstandard data structures, how would you code a wrapper? I've produced the following:...
14
by: Flzw | last post by:
Well I have a map like this : std::map <string, CObject> ObjectList; I have a function like this : CObject* NewObject( char* Name, CArg* Arg) { std::string key = Name; ObjectList =...
1
by: Mani | last post by:
Hi , I am getting a crash in std::map insert. The map object is created, but while inserting into it there is a core dump and this dosen't happen frequently. The gdb trace looks like this. ...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.