Thread: Constructors and the this pointer

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798

    Constructors and the this pointer

    Lets say we have the following class:

    Code:
    class Simple{
    int a;
    public:
    Simple (int value = 0){a = value;}
    };
    Is a this pointer supplied to the constructor? I assume not because at the time the constructor is called there is no object instantiated right? By what mechanism then is the 'a' member associated with the object in question?

    Thanks.
    Last edited by minesweeper; 06-10-2003 at 01:29 PM.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    "this" does already exist by the time the constructor is called. so the memory is valid.

    side note pertaining to your sig:

    http://www.snopes.com/quotes/bush.htm
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    >>"this" does already exist by the time the constructor is called.<<

    And is it supplied implicitly as an argument like in other methods?

    Thanks

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yes it is.

    for the life of the object you can count on having it. In the constructor or destructor, it doesn't matter. The memory must exist first and until the end. "new" simply calls the constructor after it allocates the memory. "delete" frees the memory after it has called the destructor.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    One thing to note -- If you have a base class and a derived class, the base constructor happens first, and this acts like a pointer to the BASE class, not the derived, meaning things like virtual functions will call the PARENT'S copy, not the child's.

    It is only within the child's constructor that this points to a child object.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by Cat
    One thing to note -- If you have a base class and a derived class, the base constructor happens first, and this acts like a pointer to the BASE class, not the derived, meaning things like virtual functions will call the PARENT'S copy, not the child's.

    It is only within the child's constructor that this points to a child object.
    I can understand why it would call the parent's copy anyway, because the child hasn't been defined yet. But is it really because it's a base class pointer? I thought that was the whole point of virtual functions - that the children's version will be called on a base class pointer.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Well, for a moment, before the child class exists, the pointer IS a pointer to the base class. In fact, even a abstract base class is (very momentarily) in existence.

    Essentially, the parent is "built" first, then the child. And new will ALWAYS return a pointer to the most derived class, but during the construction, the object's this pointer doesn't always refer to the most derived class (until that constructor begins).

    And yes, you can assign a child pointer to a base pointer, and use virtual vunctions, but the difference is, when you have the class fully built, you can have a base pointer pointing to a derived object. Within the parent's constructor, though, this is a base pointer pointing to the BASE object type.

Popular pages Recent additions subscribe to a feed