4

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 ?

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62

2 Answers2

9

Since the memory for the object and its members is allocated before the call of the constructor, the value of this pointer itself is not the issue: it's the members that you might dereference off of it that may be an issue.

Your first code fragment is valid, because this->m_A is identical to m_A, which is a valid expression.

Your second code fragment may or may not be OK, depending on what the constructor of SecondClass does:

  • If SecondClass constructor simply stores the pointer to FirstClass for future use, this is OK
  • If SecondClass constructor calls methods off of the pointer to FirstClass passed into it, this is not OK, because the instance to which the this pointer is pointing has not been initialized.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Also note that that you can call methods on the `this` object from your constructor, but not virtual ones - Don't know why my edit was rejected :/ – Xaqq Jul 04 '13 at 12:56
  • @Xaqq The problem with that edit was that the rule for methods is not straightforward: calls of virtuals will be routed to the implementations in the class under construction, but that's not all: if you call a non-virtual that calls a virtual, directly or indirectly, the same rule will apply as if the constructor has called the virtual directly. These details are not essential to the answer, so I left them out: I am sure that the OP will be able to find the rules in answers that specifically concentrate on this issue. – Sergey Kalinichenko Jul 04 '13 at 18:01
  • @dasblinkenlight Okay, at least there's a good explanation in your comment :D. Saksham, see dasblinkenlight comment for a deeper explanation. – Xaqq Jul 05 '13 at 07:08
1

First, yes it is totally safe to use the 'this' keyword, In case of all those members which are on stack the 'this' keyword will work fine and those which are pointer type you must assign them memory first then use them using the 'this' keyword. if you don't assign memory and try to use them then it will create an issue. Secondly you asked that how object creation takes place and when members of class are created they are created when you make the object of the class either in main or in any function.

Taimour
  • 129
  • 1
  • 1
  • 9