Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 30th, 2008, 5:59 AM   #1
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 60
Rep Power: 1 JD-Salinger is on a distinguished road
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:
c++ Syntax (Toggle Plain Text)
  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
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old Apr 30th, 2008, 7:08 AM   #2
hammerhead
Newbie
 
hammerhead's Avatar
 
Join Date: Apr 2008
Location: India
Posts: 3
Rep Power: 0 hammerhead is on a distinguished road
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
c++ Syntax (Toggle Plain Text)
  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. }
hammerhead is offline   Reply With Quote
Old Apr 30th, 2008, 7:16 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old May 1st, 2008, 12:08 AM   #4
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 60
Rep Power: 1 JD-Salinger is on a distinguished road
Re: copy constructors explanation and o.overloading

Quote:
Originally Posted by grumpy View Post
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
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old May 1st, 2008, 1:36 AM   #5
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 60
Rep Power: 1 JD-Salinger is on a distinguished road
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?
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old May 1st, 2008, 5:45 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old May 1st, 2008, 8:00 AM   #7
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 60
Rep Power: 1 JD-Salinger is on a distinguished road
Re: copy constructors explanation and o.overloading

thanks again grumpy.... now i get it......
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:08 PM.

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