I read some articles when it is said that you should not use the 'this' keyword in constructor and others saying the exact opposite....
Now my main question is : Is it safe and is it a good practice to use 'this' in a constructor ?
This question lead to others :
- How an object creation is proceed ?
- When are the members of a class created ? Before the constructor is called ?
Here is some examples working with VS2012 on windows 7 :
class FirstClass
{
int m_A;
public:
FirstClass( int a ) : m_A( a )
{
std::cout << this->m_A << std::endl;
// ^^^^
}
};
and :
class ThirdClass; // forward decl
class SecondClass
{
public:
SecondClass( ThirdClass* iTC )
{
// ...
}
};
class ThirdClass
{
SecondClass* m_SC;
public:
ThirdClass():
m_SC( new SecondClass( this ) )
// ^^^^
{
//...
}
};
Those examples are working but is there a probability to have an undefined behavior ?