copy constructor

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

copy constructor

Postby pushpa » Thu Apr 19, 2012 1:37 am

hi folks :)
I wanted to know whether a(b) of type 'test' calls copy constructor or assignment operator, but i have got compilation error (mention inline).
I have declared geti() as public but still its giving error.. I am learning c++, can anybody explain me what those error and how to fix?

Thanks in advance



Code: Select all
#include<iostream>
using namespace std;

class test
{
   int i;
   public:
   test(int x=0):i(x){}
   ~test(){}

    test(const test& y)
   {
   i = y.geti(); // error: passing ‘const test’ as ‘this’ argument of ‘int test::geti()’ discards qualifiers
   }

   test& operator=(test& m)
   {
      i = m.geti();
      return *this;
   }
   int geti()
   {
    return i;
   }
   
};
test sentobject(void)
{
   test temp(88);
   return temp;
}
int main()
{
   test a(3),b(6);
   cout<<"a(b)calls";
   a(b); //no match for call to ‘(test) (test&)’

   cout<<"a.i="<<a.geti()<<"b.i="<<b.geti()<<endl;

cout<<"sendobject calls"<<endl;
   test k = sentobject();

   return 0;
}


pushpa
 
Posts: 1
Joined: Thu Apr 19, 2012 1:29 am

Re: copy constructor

Postby exomo » Thu Apr 19, 2012 5:04 am

1. error: this is caused by a feature called const-correctness. You have defined y as a const reference. You are not allowed to call non-const methods of a const object/reference. There are two possible ways to solve this.
solution 1: make geti a const method. Just add the const keyword after the function declaration:
Code: Select all
int geti() const
    {
        return i;
    }
solution 2: don't use the geti method. Within the same class you have access to all private member variables, that includes those of a different object instance. You can just use "i = m.i;" and you're fine. That is how copy constructors are usually implemented, because you don't allways want a public get method for each member variable but still have a copy constructor.

2. error: This does neither use the copy constructor nor the assignment operator. That's why the compiler does not find a correct function to call. But the call can still be valid, if you implement the correct operator. You can use an object like a function, if you have the "function operator". e.g.
Code: Select all
    void operator() (const test& q)
    {
       cout << "function operator: " << (i + q.i) << endl;
    }
You can define the function operator for any parameters you like. The copy constructor is called when you have a variable definition that includes an assignment (like test k = sentobject();) or when you create a new object using the new instruction, like "test* test_ptr = new test(a);" Assignment operator is caled whenever you assign another object to an object that existed before.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 880
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 3 guests