dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] empty copy constructor

  1. #1
    Join Date
    Dec 2005
    Posts
    445

    [RESOLVED] empty copy constructor

    Hi,

    Can someone please explain how come the following code produce the output:

    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Hello World <-f1
    Hello World <- f2

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    class Foo
    {
    public:
    	Foo( const char* str ) { strcpy(m_str, str); }
    	Foo( const Foo& ) {}
    	
    	~Foo() { Print(); }
    	void Print() { std::cout << m_str << std::endl ; }
    
    private:
    	char m_str[128];	
    };
    
    int main()
    {
    	Foo f1( "Hello World");
    	Foo f2(f1);
    	return 0;
    }
    I've declared a copy constructor that does nothing, but when I f1 & f2 destructor is called both m_str print 'hello world' even that the copy ctor didn't do any copy.


    Many thanks!

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

    Re: empty copy constructor

    Your member variable m_str in the copy is uninitialized. Therefore the call to cout for f2 is unpredictable as to what will be printed.

    Regards,

    Paul McKenzie

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

    Re: empty copy constructor

    To be more explicit, the call to cout for f2 did print m_str. The issue is that m_str contained garbage with no NULL terminator. So the cout kept printing characters until a NULL was found. Guess where the NULL was found? It was finally reached when the cout went out of bounds of the f2::m_str, and found itself, by luck, inside of the f1::m_str.

    Second, hopefully this is just an experiment. You never write a copy constructor that on purpose or by mistake, fails to copy everything from one object to another. The only exception is if the objects are being reference counted (which still means that one object is still logically a copy of another object). If after calling a copy constructor, if one object would behave differently than the copy in any way if used interchangeably in a program, then the copy constructor is faulty.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 14th, 2009 at 05:54 PM.

  4. #4
    Join Date
    Dec 2005
    Posts
    445

    Thumbs up Re: empty copy constructor

    Thanks very much Paul for all the info.

    I thought thats the case ( it is an experiment ) but what confused me ( and still does) is that f2 is constructed AFTER f1, for some reason I thought that f2 would be allocated AFTER f1 memory address ......

    Thanks again!

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

    Re: empty copy constructor

    Quote Originally Posted by Salvadoravi View Post
    Thanks very much Paul for all the info.

    I thought thats the case ( it is an experiment ) but what confused me ( and still does) is that f2 is constructed AFTER f1, for some reason I thought that f2 would be allocated AFTER f1 memory address ......

    Thanks again!
    The objects are destroyed in the opposite order of their construction. Therefore f2 will be destroyed first, followed by f1.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Dec 2005
    Posts
    445

    Thumbs up Re: empty copy constructor

    Thanks again

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)