std::map problem with inherited classes

F

F. Meyer

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;
 
T

Thomas Tutone

F. Meyer said:
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-lite/how-to-post.html#faq-5.8

Best regards,

Tom
 
A

Axter

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.
 
T

Thomas Tutone

Axter said:
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
 
F

F. Meyer

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
;)
 
T

Thomas Tutone

F. Meyer said:
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
 
H

Howard

F. Meyer said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top