[Dev-C++] Copy constructor or assignment operator
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Maya <Esc...@ne...> - 2003-12-19 01:43:18
|
Hey folks!
I am having problems grasping the meaning of 'operator=' and copy
constructor.
In the following example
namespcace jme{
class Name{
std::string str;
public:
Name(){}
Name(const jme::Name& rhs){ this->str = rhs.str;}
~Name(){}
jme::Name& operator=(const jme::Name& rhs) {
if(this->str == rhs.str ) {
return *this;
}
this->str = rhs.str;
return *this;
}
};
class Person{
private:
jme::Name* name;
Init()throw(std::bad_alloc){
try{name = new jme::Name;}
catch(std::bad_alloc& x){ throw;}
}
public:
Person(const jme::Name& n){
try{ Init(); }
catch(std::bad_alloc& x){ throw; }
name = &n; // <<== Here is the problem
}
}
I get an error saying: Invalid conversion 'const jme::Name*' to 'jme::Name*'
If I change the parametized c'tor to jme::Person::Person(jme::Name& n) the
constantness disapears and the program compiles, but that is not what I
want.
What I want if the c'tor to receive a reference to a constant jme::Name.
Can someone please, help me understand what is that I am doing wrong.
--
FYI
http://www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of humiliation.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power.
- Malcom X
* "Innocent until proven guilty", no... not in Canada!!
|