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

    class constructor question

    I got this code:
    Code:
    #include <iostream>
    using namespace std;
    
    class test
    {
    private:
    int y;
    public:
    int z;
         test(test object)
          {
          y=object.z;
          }  
    void print_y();   
    };
    
    void test::print_y()
    {
          cout<<y<<endl;
    }
    int main()
    {
        test ob2;
        cout<<"Put whatever you want";
        cin>>ob2.z;
        test ob1(ob2);
        
        ob1.print_y();
        
    }
    I got dozens of errors like "You should use const test &". I think everything is correct with the code. why i got those errors?

    Thanks in advance.

    Regards.

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    I think everything is correct with the code.
    Obviously it's not.

    First, you can't pass test by-value in your copy constructor. Think about it, the reason should be quite obvious (HINT: what happens when you pass something by-value?).
    Second, you'll need to explicitly define a default constructor in addition to your (broken) copy constructor.
    Third, this rubbish interface can be easily replaced by providing an appropriate operator>> overload.

  3. #3
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    If I pass by-value then there is copy of the variable (in this case object of the class test), and then if in int main(), I input ob2.z=5, it should pass it on y=5. I can't see reason to use pass by reference?

  4. #4
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    If I pass by-value then there is copy of the variable (in this case object of the class test), and then if in int main(), I input ob2.z=5, it should pass it on y=5. I can't see reason to use pass by reference?
    Sigh... Let's try this again.

    What gets invoked when you pass by-value?

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    If I pass by-value then there is copy of the variable (in this case object of the class test), and then if in int main(), I input ob2.z=5, it should pass it on y=5. I can't see reason to use pass by reference?
    and where do you magically get your copy from?

  6. #6
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    There is copy created of the ob2. I should use pass by reference if I alter the ob1 variables but I don't alter them.
    For example if I need to change something in the object
    Function1(test &ob1)
    {
    ob1.z=5;
    }
    or something like that.

  7. #7
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    There is copy created of the ob2. I should use pass by reference if I alter the ob1 variables but I don't alter them.
    For example if I need to change something in the object
    Function1(test &ob1)
    {
    ob1.z=5;
    }
    or something like that.
    You're missing the ****ing point.
    When you pass T by-value, T's copy constructor gets invoked. In your case, over and over and over. In short, impossible. Passing T by (const) reference alleviates that problem.

    If you don't understand this simple concept, perhaps you should stick with shallow copying for now. Besides, that snippet in your OP doesn't require an explicit copy constructor in the first place.

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

    Re: class constructor question

    Quote Originally Posted by StGuru
    I should use pass by reference if I alter the ob1 variables but I don't alter them.
    There is another reason to use pass by reference: to avoid copying the object. If you do not want to modify the object through the reference then pass by const reference.

    Quote Originally Posted by StGuru
    There is copy created of the ob2.
    Indeed. And the act of copying invokes the copy constructor, which happens to be precisely what you are trying to implement. So what you are saying is this: I want to copy the object in order to implement copying of the object. That does not make sense.
    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. #9
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    @Plasmator, I understand what are u saying to me, but I got copy of ob2, which doesn't got constructor.
    Even if I put
    Code:
    #include <iostream>
    using namespace std;
    
    class test
    {
    private:
    int y;
    public:
    int z;
    
        test()
        {
        y=1;
        }
    
         test(test object)
          {
          y=object.z;
          }  
    void print_y();   
    };
    
    void test::print_y()
    {
          cout<<y<<endl;
    }
    int main()
    {
        test ob2;
        cout<<"Put whatever you want";
        cin>>ob2.z;
        test ob1(ob2);
        
        ob1.print_y();
        
    }
    the copy would invoke parametrized constructor and initialize y=1. I don't see a problem.

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

    Re: class constructor question

    Code:
    test ob1(ob2);
    You're passing ob2 to the copy constructor by-value.
    To pass by value the compiler uses the copy constructor, which copies its parameter by value...
    So the compiler uses the copy constructor, which copies its parameter by value...
    So the compiler uses the copy constructor, which copies its parameter by value...
    etc. etc. until something breaks.
    "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

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

    Re: class constructor question

    Quote Originally Posted by StGuru
    I got copy of ob2, which doesn't got constructor
    obj2 is default constructed.

    Quote Originally Posted by StGuru
    the copy would invoke parametrized constructor and initialize y=1. I don't see a problem.
    How did you manage to come to that conclusion?

    Look, I think you are sorely in need of an example:
    Code:
    #include <iostream>
    
    class X
    {
    public:
        X()
        {
            std::cout << "default constructor invoked" << std::endl;
        }
    
        X(const X&)
        {
            std::cout << "copy constructor invoked" << std::endl;
        }
    
        X& operator=(const X&)
        {
            std::cout << "copy assignment operator invoked" << std::endl;
            return *this;
        }
    
        ~X()
        {
            std::cout << "destructor invoked" << std::endl;
        }
    };
    
    int main()
    {
        X a;
        X b(a);
        X c;
        c = a;
    }
    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

  12. #12
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: class constructor question

    Quote Originally Posted by StGuru View Post
    @Plasmator, I understand what are u saying to me, but I got copy of ob2, which doesn't got constructor.I don't see a problem.
    Quote Originally Posted by Plasmator View Post
    When you pass T by-value, T's copy constructor gets invoked. In your case, over and over and over. In short, impossible. Passing T by (const) reference alleviates that problem.
    JohnW@Wessex's getting at the same thing.

  13. #13
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    ok, now I am totaly confused. When the object is declared then the constructor is invoked.

    Now when ob1(ob2) the constructor:
    test(test ob2)
    {
    y=object.z;
    }
    the constructor will create copy of ob2, which will invoke another unparametrized constructor in this case my constructor is:

    test()
    {
    y=1;
    }

    It is like I create two separate objects. I don't know why u say that the process is infinite.

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

    Re: class constructor question

    Quote Originally Posted by StGuru
    the constructor will create copy of ob2, which will invoke another unparametrized constructor in this case my constructor is:
    It won't. Logically, the act of copying invokes this constructor:
    Code:
    test(test ob2)
    {
    y=object.z;
    }
    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

  15. #15
    Join Date
    Mar 2009
    Posts
    82

    Re: class constructor question

    Why that constructor, when I got other unparamterized?

    Why in this code when I pass by value, there isnt invoking of the constructor of class B?
    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");
        
    }

Page 1 of 4 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