Originally Posted by
AH_Tze
Code:
Foo(int in): member(in) {
}
Ah. It works. But could you explain that syntax?
I had tried:
Code:
Foo(int in) {
member = in;
};
and gotten an "illegal assignment of read-only data member" from dev-c++.
I don't understand the difference between the two.
Imagine that member is ... a vector, and you want it to have at first 10 elements allocated. Calling
Code:
Foo(int in) {
member = vector<int>(10);
};
isn't the same as
Code:
Foo(int in) : member(10){}
Why you may ask?
When as class is declared, memory is allocated. If the class is derived, the base class constructor is called. The variables constructors are called, and last the main constructor for that class. This behaviour is extremely important because when the body of a contructor is executed, every variable must have been correctly initialized, otherwize, manipulating those variable could result fatal. Making an assignement to a int in the body isn't bad practice, but it's not also good, because it can lead to bad habits.
But making an assignement to a vector class is very bad!!! Because when your constructor is executed your vector class is already constructed. Assingning it another value through '=' implies creating a new temporary vector, calling operator=(), then deconstructing the temporary vector. This is EXTREMELY ineficient.
EDIT: here's an example of this behaviour
Code:
#include <iostream>
class A{
int val;
public:
A(int n=0) : val(n){std::cout << "A constructed and val "<<val<<std::endl;}
~A(){std::cout << "A deconstructed and val "<<val<<std::endl;}
A& operator=(const A& a){
val=a.val;
std::cout << "A operator=() called and val "<<val<<std::endl;
return *this;}
};
class B{
int val;
public:
B(int n=1) : val(n){std::cout << "B constructed and val "<<val<<std::endl;}
~B(){std::cout << "B deconstructed and val "<<val<<std::endl;}
};
class C{
int val;
public:
C(int n=2) : val(n){std::cout << "C constructed and val "<<val<<std::endl;}
~C(){std::cout << "C deconstructed and val "<<val<<std::endl;}
};
class D: public C{
public:
A a;
B b;
D(){
std::cout << "D constructed\n";
a = A(10);
b = B(56);
}
~D(){std::cout << "D deconstructed"<<std::endl;}
};
int main(){
D d;
return 0;
}
And the output
Code:
//beginning - D class initialization
C constructed and val 2 //base C class of D constructed
A constructed and val 0 //class A var in D constructed
B constructed and val 1 //class B var in D constructed
D constructed //D constuctor executed
A constructed and val 10 //temporary A class for assignement created
A operator=() called and val 10 //a assigned
A deconstructed and val 10 //temporary var desconstructed
B constructed and val 56 //temporary B class for assignement
B deconstructed and val 56 //temporary B class deconstructed
// - My compiler seemed to create a default B::operator=()
//I dont' know if this is standard behaviour
//end
D deconstructed
B deconstructed and val 56
A deconstructed and val 10
C deconstructed and val 2
If you call the constructors for the variables, recomended, instead of assigning values to then, like showed above, the output would be
Code:
C constructed and val 2
A constructed and val 10
B constructed and val 56
D constructed
D deconstructed
B deconstructed and val 56
A deconstructed and val 10
C deconstructed and val 2
Each constructor called only once. Good behaviour