CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    Join Date
    Jan 2001
    Posts
    25

    A question about constructor function

    class T
    {
    public:
    int m_i ;
    T () { m_i = 1 ; }
    T(int k ) { T() ; }
    };

    int main()
    {
    T t(0 );
    cout<< t.m_i <<endl;
    return 0;
    }
    When i invoke a constructor funciton within another constructor funciton , this constructor will not impact on this object .

    In the above sample code , The t.m_i will remain uninitialize . why
    ? i test it in VC6.

    I have gone throught the ISO C++ specificaiton , but no any thread was been found .

  2. #2
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    You shouldn't call another constructor inside a constructor but a reset-function in both:
    Code:
    class T
    {
    public:
    int m_i ;
    T () { reset() ; } 
    T(int k ) { reset(); }
    
    private:
    void reset(){m_i = 1 ;}
    };
    (And just for design: You shouldn't put variables in the public-section, but implement get/set-functions for that purpose.)

    Mikey

  3. #3
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    (But to Your question: I really do not know, why m_i stays uninitilized. Maybe because it's not legal to call a constructor directly.)

    Mikey

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

    Re: A question about constructor function

    Originally posted by flysnow
    When i invoke a constructor funciton within another constructor funciton , this constructor will not impact on this object .

    In the above sample code , The t.m_i will remain uninitialize . why
    ? i test it in VC6.
    The reason is quite simple...the call to the first constructor inside your second one will create a temporar instance of 'T' which is only valid for the current scope thus only until the second constructor returns...

    You can easily see what happens while debugging through the construction of the instance of 'T'...take a look at the 'this' pointers...the following code demonstrates the problem as well...just run it and look at the output...
    Code:
    #include <iostream>
    #include <iomanip>
    
    class T
    {
    public:
      int m_i ;
    
      T()
      {
        std::cout << "First constructor = 0x" << std::hex << this << std::endl;
        m_i = 1 ;
      }
    
      T(int k) 
      {
        std::cout << "Second constructor = 0x" << std::hex << this << std::endl;
        T();
      }
    
      ~T()
      {
        std::cout << "Destructor = 0x" << std::hex << this << std::endl;
      }
    };
    
    
    int main()
    {
      {
        T t(0);
        std::cout << "Instance created" << std::endl;
      }
    
      return 0;
    }

  5. #5
    Join Date
    Jan 2001
    Posts
    25

    Are there some c++ expert here ?

    I know the result , but why? why was the temporary object created ?
    Is it a normal behavior defined by C++ specificaiton ?


    Thanks Mikey and Andreas Masur .


    The code is just for describing the problem . so i put the m_i public.

  6. #6
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    I know the result , but why? why was the temporary object created ?
    Andreas is absolutely right, that You create a temporary object instead of calling Your own default-constructor.

    It's really quite simple:

    You create an object of T with T() (default-ctor). It doesn't matter if You write this inside or outside Your class.

    You can prove this by comment out Your default-ctor-definition:
    Code:
    class T
    {
    public:
    int m_i ;
    // T () { m_i = 1 ; } no default-constructor!
    T(int k ) { T() ; } // try to create a T-object through the default-ctor
    };
    
    int main()
    {
    T(); // the same: creating a T-object through default-ctor
    T t(0 );
    cout<< t.m_i <<endl;
    return 0;
    }
    this raises the error:
    ... error C2512: 'T::T' : no appropriate default constructor

    This is standard C++-behaviour

    Mikey
    Last edited by Mikey; December 1st, 2002 at 10:28 AM.

  7. #7
    Join Date
    Jan 2001
    Posts
    25
    Mikey:

    Thanks!

    Can I called a constructor funciton directly to create a object ?

    The answer is Yes by your sample code .

    but why the C++ need this feature ? Because you can not access this temporary object by any way.

  8. #8
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    You can create an object, without saving it to a var, if You only need a constructor-execution:
    Code:
    class T
    {
    	public:
    	T() 
    	{
    		OutputDebugString("i was called, to do some work!\n");
    	}
    };
    
    int main()
    {
    	T(); // output ctor-text
    	return 0;
    }
    Output:
    i was called, to do some work


    But there are very rare cases, where this makes sense

    Mikey

  9. #9
    Join Date
    Jan 2001
    Posts
    25
    Mikey :
    Thanks very much

    If you want to do it like what you mentioned , why don't use a stactic function or a globle function ? A instantiation of object is very costly.

    I just want to know the roof cause

  10. #10
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    Originally posted by flysnow
    If you want to do it like what you mentioned , why don't use a stactic function or a globle function ? A instantiation of object is very costly.
    Yes, of course a static function or whatever would be better.

    As I said, there are very rare cases for the need of an object without var. (And currently I can't imaging one )

    It was just an example, how You can use it.

    Mikey

  11. #11
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    A useful case would be to return it from a function:
    Code:
    CString getString(LPCSTR sz)
    {
    	// create 2 temp. objects (return the first one)
    	return CString(CString(sz)/*temp. obj to use the '+'-operator*/ + "hello");
    }
    
    
    CString cs = getString("hi and "); // cs="hi and hello"

    Mikey
    Last edited by Mikey; December 1st, 2002 at 12:38 PM.

  12. #12
    Join Date
    Jan 2001
    Posts
    25
    Mikey :

    Do you know who is the most famous expert in this C++ board ? I think only the C++ expert can answer this questin .

  13. #13
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    I think Your question was already answered some mails ago...

    Mikey

  14. #14
    Join Date
    Jan 2001
    Posts
    25
    Originally posted by Mikey
    A useful case would be to return it from a function:
    Code:
    CString getString(LPCSTR sz)
    {
    	// create 2 temp. objects (return the first one)
    	return CString(CString(sz)/*temp. obj to use the '+'-operator*/ + "hello");
    }
    
    
    CString cs = getString("hi and "); // cs="hi and hello"

    Mikey
    But I think it isn't the exact explanation for my question , If you traced the assemble code of my sampe code in VC , you will find that a destuctor function was called immediately after construtor function was called on the temporary object .

    So I don't think it can return a object by that way .

  15. #15
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    Since You don't save Your object in a var, it's destroyed after use.

Page 1 of 3 123 LastLast

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