CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 4 FirstFirst 1234
Results 46 to 57 of 57
  1. #46
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Here is the code that I worked with:

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    class test
    {
     int c;
     
    friend void funkcija(test);
    
     public:
     
    int d;
               test(){ cout<<"Unparametrized constructor."<<endl;}
            
               test(const test &objekat)
                   {
                     cout<<"Copy constructor called."<<endl;
                     
                   }
     
    
    };
    
    void funkcija(test objekat){
    
    
    cout<<objekat.c<<endl;
    cout<<objekat.d;
    
    
    }
    int main()
    {
        test ob;
        
        funkcija(ob);
        
        system("PAUSE");
        
        return 0;
    }
    The copy constructor is made to make deep copy of the object. So to make sure that the members of the class test (c,d) exist I printed their values in the function (no matter the printed text is non-understandable, I can still change their values (for ex. object.c=5 or object.d=33) so they obviously exist.

  2. #47
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: class constructor question

    Quote Originally Posted by StGuru
    So to make sure that the members of the class test (c,d) exist I printed their values in the function (no matter the printed text is non-understandable, I can still change their values (for ex. object.c=5 or object.d=33) so they obviously exist.
    Then show an example where you actually initialise the member variable (only d, since you cannot initialise c from the main function), then in foo the member variable is correctly copied despite the lack of a correct implementation of the copy constructor.

    Looking at the lack of initialisation, my guess is that you are mistaken: the member variables are not being copied, but you fooled yourself into believing they were copied.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #48
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Quote Originally Posted by laserlight View Post
    Then show an example where you actually initialise the member variable (only d, since you cannot initialise c from the main function), then in foo the member variable is correctly copied despite the lack of a correct implementation of the copy constructor.

    Looking at the lack of initialisation, my guess is that you are mistaken: the member variables are not being copied, but you fooled yourself into believing they were copied.
    Actually, you're right. I misunderstood the terms 'copy' and 'create' :-).

    I got an interesting problem.
    Code:
    test func(test object)
    {
    return object;
    }
    test ob=func(ob1);
    We got several callings of the copy constructor.

    I - object is passed by value as a parameter to a function

    II - object is returned from a function

    III- object is created from another object of the same type

    But why the copy constructor of test ob=func(ob1); is called first, where there is nothing to copy?

  4. #49
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: class constructor question

    Quote Originally Posted by StGuru
    But why the copy constructor of test ob=func(ob1); is called first, where there is nothing to copy?
    What do you mean? Once again, I suggest that you post a small and simple compilable program that demonstrates what you are talking about. Post the output you get as well, keeping in mind that the compiler may elide copy construction in some cases.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #50
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Thanks for the suggestions. Here is the code:
    Code:
    #include <iostream>
    using namespace std;
    
    
    class test
    {
     int c;
     
     friend test funkcija(test);
     
     public:
            
          test(){ cout<<"Non-parametric constructor called."<<endl;}
     
            
          test(const test &objekat)
          {
                     cout<<"Copy constructor called."<<endl;
                     
          }
     int d;
    
    };
    
    test funkcija(test objekat)
    { return objekat; }
    
    int main()
    {
        test ob;
        
        ob.d=5;
        
        test ob1=funkcija(ob);
        
        system("PAUSE");
        
        return 0;
    }

  6. #51
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: class constructor question

    Okay, so what is the expected and actual output?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #52
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    I expect the copy constructor to be called three times, but its called 2 times. Why?

  8. #53
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: class constructor question

    Quote Originally Posted by StGuru
    I expect the copy constructor to be called three times, but its called 2 times. Why?
    Because one of those copy constructor invocations that you expected was elided by the compiler. (In particular, there is no need to create a temporary to be copied to the object to be initialised since the object to be initialised can be initialised as a copy of the local variable.)
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #54
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    I expect the copy constructor to be called three times, but its called 2 times. Why?
    Search for 'return value optimization'
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

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

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    I expect the copy constructor to be called three times, but its called 2 times. Why?
    Go back to my post #21:
    Also note that copy construction can be removed by the compiler if it is detected that the copy is superfluous
    In other words, you can't predict how many times a copy constructor or assignment operator is called when given code that can generate multiple calls to these functions.

    That's why when you write a user-defined assignment operator or copy constructor, you can't write it with the expectation it will be called a certain number of times. These operations could be called once, twice, three times, ten times, or no times, given the same piece of code and compiled using different compilers and/or compiler options.

    Regards,

    Paul McKenzie

  11. #56
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Does it mean that the compiler does not create copy for the returning object since it already been copied (before passed by value as a parameter to a function and copy constructor called)?

  12. #57
    Join Date
    Nov 2006
    Posts
    1,611

    Re: class constructor question

    Does it mean that the compiler does not create copy....

    You've got the general idea. As a theme you can expect that modern compiler optimizations take every effort to fashion results which "mean" exactly the same thing as what you've written, but with as few steps as possible, given the level of research. Compilers have improved dramatically over the years.

    Even when optimizations are turned off, some of the very obvious ones are still used, like genuinely superfluous copies.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

Page 4 of 4 FirstFirst 1234

Tags for this Thread

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