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

    Re: class constructor question

    I mean:
    Code:
    #include <iostream>
    using namespace std;
    class B
    {
    public:
    B()
       {
          cout<<"test";
          }
    int n;
    };
    class test
    {
    private:
    int y;
    public:
    int z;
        test()
        {
        y=1;
        }
         test(B object)
          {
          y=object.n;
          }  
    void print_y();   
    };
    
    void test::print_y()
    {
          cout<<y<<endl;
    }
    int main()
    {
        B ob2;
        B ob3;
        cout<<"Put whatever you want";
        cin>>ob2.n;
        test ob1(ob2);
        
        ob1.print_y();
        
        system("PAUSE");
        
    }
    Here I got a copy of the object, and a copy constructor. Will be there copy of the object?

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

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    Here I got a copy of the object, and a copy constructor.
    First, can you please format your code a little better? It is very hard to read when it's staggered all over the place.

    Second, you do not have a user-defined copy constructor. You have a default constructor, not a uder-defined copy constructor.

    A copy constructor has the following prototype:
    Code:
    B(const B&);
    There are other signatures that are valid copy constructors, but this is the one that is mainly used. All of them require a reference to a B object as the parameter.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 28th, 2009 at 03:54 AM.

  3. #33
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Sorry, I put the wrong code. Here is the new one:
    Code:
    #include <iostream>
    using namespace std;
    class B
    {
    public:
    B(const B &object)
       {
          cout<<"test";
          }
    int n;
    };
    class test
    {
    private:
    int y;
    public:
    int z;
        test()
        {
        y=1;
        }
         test(B object)
          {
          y=object.n;
          }  
    void print_y();   
    };
    
    void test::print_y()
    {
          cout<<y<<endl;
    }
    int main()
    {
        B ob2(ob2);
        B ob3(ob3);
        cout<<"Put whatever you want";
        cin>>ob2.n;
        test ob1(ob2);
        
        ob1.print_y();
        
        system("PAUSE");
        
    }

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

    Re: class constructor question

    StGuru, please put in more effort to indent and format your code properly. For example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class B
    {
    public:
        B(const B &object)
        {
            cout << "test";
        }
    
        int n;
    };
    
    class test
    {
    public:
        int z;
    
        test()
        {
            y = 1;
        }
    
        test(B object)
        {
            y = object.n;
        }
    
        void print_y();
    private:
        int y;
    };
    
    void test::print_y()
    {
        cout << y << endl;
    }
    
    int main()
    {
        B ob2(ob2);
        B ob3(ob3);
    
        cout << "Put whatever you want";
        cin >> ob2.n;
    
        test ob1(ob2);
        ob1.print_y();
    
        system("PAUSE");
    }
    Note that you should #include <cstdlib> for std::system(), but it is not necessary anyway.

    So, what is your question again?
    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. #35
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Will be there copy of the object since I defined my own copy constructor just to print "test"?

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

    Re: class constructor question

    Quote Originally Posted by StGuru
    Will be there copy of the object since I defined my own copy constructor just to print "test"?
    Yes, unless the compiler is able to elide (as in remove) the copying. Whether the copying is correct is another matter.

    By the way, I am not sure what to make of this:
    Code:
    B ob2(ob2);
    B ob3(ob3);
    You don't need ob3 in your example, and you should default construct ob2. You need to define the default constructor since you have declared the copy constructor (the default constructor is only generated by the compiler when there are no user declared constructors).
    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. #37
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Your right about the objects/ I just need to define default constructor.

    Anyway, my copy constructor should copy the object. For example:

    //=== file Point.h =============================================
    class Point {
    public:
    . . .
    Point(const Point& p); // copy constructor
    . . .
    //=== file Point.cpp ==========================================
    . . .
    Point::Point(const Point& p) {
    x = p.x;
    y = p.y;
    }
    . . .
    //=== file my_program.cpp ====================================
    . . .
    Point p; // calls default constructor
    Point s = p; // calls copy constructor.
    p = s; // assignment, not copy constructor.

    would copy the variables of the object. And in my case it would just print "test". That's what I am asking.

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

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    And in my case it would just print "test". That's what I am asking.
    Hopefully this is just an exercise. When you take on the responsibility of writing your own copy constructor, you better be telling the truth to the compiler -- you are actually making a valid copy of the object. You are not doing that in your copy constructor, since you had a variable "n" that was never copied.

    If you don't do this, what you'll end up with are two objects, where one is a partial or bogus copy of the other. What ends up happening is that at runtime, you are susceptible to errors that are very difficult to spot. Basically the rule to follow is this:

    You have two objects, A and B, and B is a copy of A. Wherever in the program I use A, if I replace A with B, the program must show the same exact behaviour. If using B results in different behaviour than A, then you don't have a copy, and you're attempting to shoot yourself in the foot.

    Secondly, you only code a user-defined copy constructor when you have to. According to your class, the default copy constructor generated by the compiler is adequate and you need not write your own copy constructor. Again, if you're wrong in the way you've coded your copy constructor, the compiler will not warn you. So if you don't need to write a user-defined copy constructor, don't write one.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 28th, 2009 at 02:47 PM.

  9. #39
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Thanks for the replies.

    And how does the copy constructor looks like in the compiler?

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

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    Thanks for the replies.

    And how does the copy constructor looks like in the compiler?
    It depends on the compiler and the members that are to be copied.

    Regards,

    Paul McKenzie

  11. #41
    Join Date
    Apr 2009
    Posts
    47

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    Your right about the objects/ I just need to define default constructor.

    Point p; // calls default constructor
    Point s = p; // calls copy constructor.
    p = s; // assignment, not copy constructor.

    would copy the variables of the object. And in my case it would just print "test". That's what I am asking.
    dude ...

    first line: ok
    second line NOT OK. 'Point s' just calls the default, argumentless constructor, then the following 's = p' calls operator=. Copy constructor is called when 'Point s(p)'
    third line: ok

    Quote Originally Posted by StGuru View Post
    kellogs it helped me. Thank you very much for the help. I own u beer! :-). What I can't understand now is, will the same do
    B(const B &object)
    {
    cout<<"test";
    }
    or it will not create object and will print test?
    Ok u give me the beer! ^^
    But I am not conviced of ur understanding. Say why dont u read some good programin' book or tutorial ? Cant recommend u onne though on the spot. Maybe others .

    Cheers

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

    Re: class constructor question

    Quote Originally Posted by kellogs
    second line NOT OK. 'Point s' just calls the default, argumentless constructor, then the following 's = p' calls operator=. Copy constructor is called when 'Point s(p)'
    Actually, StGuru is correct. The second line invokes the copy constructor, not the default constructor and copy assignment operator.
    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

  13. #43
    Join Date
    Apr 2009
    Posts
    47

    Re: class constructor question

    Quote Originally Posted by laserlight View Post
    Actually, StGuru is correct. The second line invokes the copy constructor, not the default constructor and copy assignment operator.
    I'll be, tested it, StGuru was correct indeed. Okay I shall pass the beer from StGuru along, laserlight

  14. #44
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    I was doing a little research and find out when:

    I - When an object is created from another object of the same type if I define the copy constructor as (for ex. test ob=ob1):
    Code:
    test(const test &objekat)
          {
                     cout<<"Copy constructor is called."<<endl;
                     
          }
    the values of ob1 would not be copied to ob and "Copy constructor is called." would be printed.

    II - When an object is passed by value as a parameter to a function and the copy constructor defined as:
    Code:
    test(const test &objekat)
          {
                     cout<<"Copy constructor is called."<<endl;
                     
          }
    then the members of the class would be copied (no matter what is defined in the copy constructor) and "Copy constructor is called." would be printed.

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

    Re: class constructor question

    That is interesting. This difference does not sound correct to me, and a quick check with g++ 4.2.4 (with and without optimisations turned on) for this test program:
    Code:
    #include <iostream>
    #include <string>
    
    class X
    {
    public:
        explicit X(const std::string s = std::string()) : s(s) {}
    
        X(const X& other)
        {
            std::cout << "X copy ctor" << std::endl;
        }
    
        std::string get() const
        {
            return s;
        }
    private:
        std::string s;
    };
    
    void foo(X x)
    {
        std::cout << x.get() << std::endl;
    }
    
    int main()
    {
        X a("test");
    
        std::cout << "Test #1: " << std::endl;
        X b = a;
        std::cout << b.get() << std::endl;
    
        std::cout << "Test #2: " << std::endl;
        foo(a);
    }
    gives me the output of:
    Code:
    Test #1:
    X copy ctor
    
    Test #2:
    X copy ctor
    Changing the copy constructor implementation to:
    Code:
    X(const X& other) : s(other.s)
    {
        std::cout << "X copy ctor" << std::endl;
    }
    gave me the output of:
    Code:
    Test #1:
    X copy ctor
    test
    Test #2:
    X copy ctor
    test
    Either way, I did not detect the difference that you described.
    Last edited by laserlight; June 30th, 2009 at 10:38 AM.
    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

Page 3 of 4 FirstFirst 1234 LastLast

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