Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   copy constructors explanation and o.overloading (http://www.programmingforums.org/showthread.php?t=15733)

JD-Salinger Apr 30th, 2008 5:59 AM

copy constructors explanation and o.overloading
 
I just need some little explanation about this snippet, i did some little experiment in the class "value" and when i compiled the source file, error pops out, even though in my own logic, there is nothing wrong with it, i came with this book and im trying to learn copy constructors and operator overloading and i did some experimenting on the code:

This is the original code:
:

  1. #include  <iostream>
  2. #include  <sstream>
  3.  
  4. using namespace std;
  5.  
  6. class Value
  7. {
  8.     public:
  9.         Value()
  10.         {
  11.             cout<<" Default constructor\n";
  12.             _numString=new char[1];
  13.             _numString[0]= '\0';
  14.         }
  15.  
  16.         Value(int i)
  17.         {
  18.             cout<< "Construction/conversion from int "<<i<<endl;
  19.  
  20.             stringstream buffer;
  21.             buffer<< i <<ends; //terminate sring
  22.             Init(buffer.str().c_str());
  23.             Display();
  24.         }
  25.  
  26.         Value(Value const& v)
  27.         {
  28.             cout<< " Copy constructor("
  29.                 << v._numString<< ")\n";
  30.             Init(v._numString);
  31.             Display();
  32.         }
  33.  
  34.         Value& operator= (Value const& v)
  35.         {
  36.             cout<<"operator=("
  37.                 <<v._numString
  38.                 <<")\n";
  39.  
  40.             if(_numString != v._numString)
  41.             {
  42.                 cout<<"not equal"<<endl;
  43.                 delete _numString;
  44.                 Init(v._numString);
  45.             }
  46.             Display();
  47.             return *this;
  48.         }
  49.  
  50.         friend Value operator+ (Value const& v1,Value const& v2)
  51.         {
  52.             cout<<"  operator+ ("<<v1._numString<< ", "
  53.                 <<v2._numString <<")\n";
  54.  
  55.             stringstream buffer;
  56.             buffer<<v1._numString<< " + "<<v2._numString<<ends;
  57.             Value result;
  58.             result.Init(buffer.str().c_str());
  59.             cout<<" Returning by value\n";
  60.             return result;
  61.         }
  62.  
  63.     private:
  64.  
  65.         void Init(char const* buf)
  66.         {
  67.             int len=strlen(buf);
  68.             _numString=new char[len + 1];
  69.             strcpy(_numString,buf);
  70.         }
  71.  
  72.         void Display()
  73.         {
  74.             cout<< "\t" <<_numString<<endl;
  75.         }
  76.  
  77.         char* _numString;
  78. };


now what I did is that on line 50 i replaced it to
inline Value operator+ (Value const& v1,Value const& v2)
then the error is "must take zero to one argument"

and i also replaced line 50 to
Value& operator+ (Value const& v1,Value const& v2)
and still same error....

im wondering what's the explanation behind this, i just copied line 34... Thanks

hammerhead Apr 30th, 2008 7:08 AM

Re: copy constructors explanation and o.overloading
 
I am not sure but you can try rewriting your code from line 50 as keep in mind to remove the friend keyword
:

  1. Value operator+ (Value const& v2)
  2.         {
  3.             cout<<"  operator+ ("<<_numString<< ", "
  4.                 <<v2._numString <<")\n";
  5.  
  6.             stringstream buffer;
  7.             buffer<<_numString<< " + "<<v2._numString<<ends;
  8.             Value result;
  9.             result.Init(buffer.str().c_str());
  10.             cout<<" Returning by value\n";
  11.             return result;
  12.         }


grumpy Apr 30th, 2008 7:16 AM

Re: copy constructors explanation and o.overloading
 
operator+() can be a member function of the class that accepts one argument, so "x = a + b" is effectively "x = a.operator+(b)" or a non-member, so "x = a + b" is effectively "x = operator+(a, b);".

The member function version can only accept one argument. The non-member version can only accept two.

The friend declaration within the class declares the non-member version as a friend of the class, which accepts two arguments. Removing the friend keyword makes the declaration into a member function, which is only allowed to accept one argument. However, your declaration has two .... hence the error.

The inline keyword is unrelated to your error.

JD-Salinger May 1st, 2008 12:08 AM

Re: copy constructors explanation and o.overloading
 
Quote:

Originally Posted by grumpy (Post 144563)
operator+() can be a member function of the class that accepts one argument, so "x = a + b" is effectively "x = a.operator+(b)" or a non-member, so "x = a + b" is effectively "x = operator+(a, b);".

The member function version can only accept one argument. The non-member version can only accept two.

The friend declaration within the class declares the non-member version as a friend of the class, which accepts two arguments. Removing the friend keyword makes the declaration into a member function, which is only allowed to accept one argument. However, your declaration has two .... hence the error.

The inline keyword is unrelated to your error.

wow!!!!.... c++ is really complicated... have to read more... and get some experience in real life programming....Tnx Grumpy... you are really a guru

JD-Salinger May 1st, 2008 1:36 AM

Re: copy constructors explanation and o.overloading
 
anway that means that operator+() can only be used to accept one argument? so everytime you overload an operator you'll always think that it must accept one argument?

grumpy May 1st, 2008 5:45 AM

Re: copy constructors explanation and o.overloading
 
Addition is a binary operation: it acts on two objects and returns a result.

The only difference between implementing an operator+() as a member function or as a non-member function is how the two objects are passed to the function.

Unrelated to the topic: operator+(), like operator-() can also be a unary operation.

JD-Salinger May 1st, 2008 8:00 AM

Re: copy constructors explanation and o.overloading
 
thanks again grumpy.... now i get it......


All times are GMT -5. The time now is 9:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC