CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    A constructor calls another constructor of the same class?

    class Foo {
    public:
    Foo(char x);
    Foo(char x, int y);
    ...
    };

    Foo::Foo(char x)
    {
    ...
    Foo(x, 0); // this line does NOT help initialize the this object!!
    ...
    }

    My questions is why Foo(x,0) doesn't help initialize the this object? Thanks for your suggestions.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A constructor calls another constructor of the same class?

    Quote Originally Posted by dullboy
    Foo::Foo(char x)
    {
    ...
    Foo(x, 0); // this line does NOT help initialize the this object!!
    ...
    }

    My questions is why Foo(x,0) doesn't help initialize the this object? Thanks for your suggestions.
    Because Foo(x,0) is a brand-new temporary object. It has no influence on the current object.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: A constructor calls another constructor of the same class?

    Code:
    class foo
    {
     char m_x;
    public:
      foo(char x = 0); // 0 is the default value for x
      void print();
    }
    
    foo::foo(char x) : m_x(x) // m_x = x
    {
    }
    
    void foo:print()
    {
      cout << m_x << endl;
    }
    In this case you can create an object
    Code:
    foo f1;
    f1.print();  // prints 0
    
    foo f2(2);
    f2.print(); // prints 2
    And please use the C++ (Non VisualC++ Issues) Forum for non visual questions.
    Last edited by cilu; January 26th, 2005 at 11:42 AM.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Aug 2000
    Posts
    1,471

    Re: A constructor calls another constructor of the same class?

    Thanks for your reply. Perhaps my question should be that how a constructor of a class constructs "this" object?
    Quote Originally Posted by Paul McKenzie
    Because Foo(x,0) is a brand-new temporary object. It has no influence on the current object.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: A constructor calls another constructor of the same class?

    this is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Aug 2000
    Posts
    1,471

    Re: A constructor calls another constructor of the same class?

    Thanks for your reply. But I can't see your answer is related to my question. Please advise. I think often people here are more helpful. That is why I prefer posting my questions here.
    Quote Originally Posted by cilu
    Code:
    class foo
    {
     char m_x;
    public:
      foo(char x = 0); // 0 is the default value for x
      void print();
    }
    
    foo::foo(char x) : m_x(x) // m_x = x
    {
    }
    
    void foo:print()
    {
      cout << m_x << endl;
    }
    In this case you can create an object
    Code:
    foo f1;
    f1.print();  // prints 0
    
    foo f2(2);
    f2.print(); // prints 2
    And please use the C++ (Non VisualC++ Issues) Forum for non visual questions.

  7. #7
    Join Date
    Aug 2000
    Posts
    1,471

    Re: A constructor calls another constructor of the same class?

    Actually, what I am concerned about at this point is how "this" is initialized in the constructor.
    Quote Originally Posted by cilu
    this is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A constructor calls another constructor of the same class?

    Quote Originally Posted by dullboy
    Actually, what I am concerned about at this point is how "this" is initialized in the constructor.
    I don't understand your question, or your concern for that matter. In the body of the constructor, this is valid.

    I don't know what else to tell you. The compiler does what it does. Just follow the rules -- you're constructing a new object in C++ when you invoke the constructor, regardless of where you may be constructing the new object. The fact that you are constructing a Foo() object in the Foo() constructor is just a matter of consequence -- nothing special or extraordinary is occuring.

    Also, this question should be posted in the non-Visual C++ section. There is just as much help there (maybe more so), when it comes to general C++ questions.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Aug 2000
    Posts
    1,471

    Re: A constructor calls another constructor of the same class?

    Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
    Quote Originally Posted by Paul McKenzie
    I don't understand your question, or your concern for that matter. In the body of the constructor, this is valid.

    I don't know what else to tell you. The compiler does what it does. Just follow the rules -- you're constructing a new object in C++ when you invoke the constructor, regardless of where you may be constructing the new object. The fact that you are constructing a Foo() object in the Foo() constructor is just a matter of consequence -- nothing special or extraordinary is occuring.

    Also, this question should be posted in the non-Visual C++ section. There is just as much help there (maybe more so), when it comes to general C++ questions.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A constructor calls another constructor of the same class?

    Quote Originally Posted by dullboy
    Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
    From what it looks like you're trying to do, why not just have another function that does what you want, and call it from the constructor? Maybe call it Init() or something like that.
    Code:
    class Foo 
    {
      public:
         Foo(char x)
         {
              Init(x, 0);
         }
    
         Foo(char x, int y)
         {
             Init(x, y);
         }
      
      private:
         void Init(char x, int y)
         {
           // whatever
         }
    };
    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: A constructor calls another constructor of the same class?

    Follow Paul McKenzies' suggestion.
    In C++ you cannot call another constructor for the same object from within that object.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: A constructor calls another constructor of the same class?

    [ Moved thread ]

  13. #13
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: A constructor calls another constructor of the same class?

    What about this approach?

    Code:
    class Foo  
    {
    public:
    	Foo(char x, char y = 0);
    
    	virtual ~Foo();
    	
    	char m_x;
    	char m_y;
    };
    
    
    
    Foo::Foo(char x, char y/* = 0*/) 
    	: m_x(x)
    	, m_y(y)
    {
    }
    
    Foo::~Foo()
    {
    }
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  14. #14
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: A constructor calls another constructor of the same class?

    Quote Originally Posted by dullboy
    Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
    The way I see it, is that you don't know what "this" is. I have to repeat myself. this is not another member object of the class. this is a const pointer, which is the address of the object whose method is called. When you call the constructor (as well as any other method) the address of the object is passed as a hidden parameter. And you can use this pointer to refer to the object itself. So you cannot speak about a special initialization of this pointer, rather than the object. You must see this as an alter ego of the object.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  15. #15
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: A constructor calls another constructor of the same class?

    Quote Originally Posted by dullboy
    Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
    Possibly the reason for the confusion is in that statement. Do you mean that you want to set the value of this? I.e. that you want to set the address of the object? Or that you want to explicitly initialise *this - i.e. the member variables of the object pointed to?

    For the former, the only way to do that is to use placement new - i.e. allocate some storage and construct the object there.

    For the latter: well, that's exactly what the constructor is doing, and this is where the confusion may be creeping in - you seem to be asking how to do something that you are already doing. The suggestions given so far should help.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured