Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator overloading and copy constructor. Can't find the error.

Reply
Thread Tools

Operator overloading and copy constructor. Can't find the error.

 
 
clicwar
Guest
Posts: n/a
 
      07-22-2007
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;

class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector(void);
Vector operator+ (Vector a);
Vector(Vector &source);
void Show(void);
};
void Vector::Show(void) {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector(float u, float v) {
x=u; y=v;
}
Vector::Vector(void) {
x=0; y=0;
}
Vector::Vector(Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector:perator+ (Vector a) {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}

int main(void) {
Vector a(3,1), b(5,2), c, d;
c = a+b;
d = a.operator+ (b);
cout << "Data members of the vector c: ";
c.Show();
Vector e(a+b);
cout <<endl << "Data members of the vector e: ";
e.Show();

return 0;
}


The compiler (g++ -pedantic -W -Wall) says:
teste.cpp: In function `int main()':
teste.cpp:36: error: no matching function for call to
`Vector::Vector(Vector)'
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:37: error: no matching function for call to
`Vector::Vector(Vector)'
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:40: error: no matching function for call to
`Vector::Vector(Vector)'
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:21: note: Vector::Vector()
teste.cpp:18: note: Vector::Vector(float, float)


Without the copy constructor Vector::Vector(Vector &source) , it works
fine.

Would anyone know what is wrong in the code?
Thanks in advance.

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      07-22-2007
"clicwar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> A simple program with operator overloading and copy constructor:
> #include <iostream>
> #include <string>
> using namespace std;
>
> class Vector {
> private:
> float x,y;
> public:
> Vector(float u, float v);
> Vector(void);
> Vector operator+ (Vector a);
> Vector(Vector &source);
> void Show(void);
> };
> void Vector::Show(void) {
> cout <<"(" << x <<"," <<y <<")";
> }
> Vector::Vector(float u, float v) {
> x=u; y=v;
> }
> Vector::Vector(void) {
> x=0; y=0;
> }
> Vector::Vector(Vector &source) {
> x = (source.x)*2 ; y = (source.y)*2 ;
> }
> Vector Vector:perator+ (Vector a) {
> Vector temp;
> temp.x = x + a.x;
> temp.y = y + a.y;
> return temp;
> }
>
> int main(void) {
> Vector a(3,1), b(5,2), c, d;
> c = a+b;
> d = a.operator+ (b);
> cout << "Data members of the vector c: ";
> c.Show();
> Vector e(a+b);
> cout <<endl << "Data members of the vector e: ";
> e.Show();
>
> return 0;
> }
>
>
> The compiler (g++ -pedantic -W -Wall) says:
> teste.cpp: In function `int main()':
> teste.cpp:36: error: no matching function for call to
> `Vector::Vector(Vector)'
> teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
> teste.cpp:37: error: no matching function for call to
> `Vector::Vector(Vector)'
> teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
> teste.cpp:40: error: no matching function for call to
> `Vector::Vector(Vector)'
> teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
> teste.cpp:21: note: Vector::Vector()
> teste.cpp:18: note: Vector::Vector(float, float)
>
>
> Without the copy constructor Vector::Vector(Vector &source) , it works
> fine.
>
> Would anyone know what is wrong in the code?
> Thanks in advance.


Change it to:
Vector::Vector( const Vector& a )
and it should work.


 
Reply With Quote
 
 
 
 
clicwar
Guest
Posts: n/a
 
      07-22-2007
First of all, thank you Jim. It worked ( but i don't know why the
simple addition of the const modifier can do that. Could you tell
me? ).

The output now is:
Data members of the vector c: (13,5)
Data members of the vector e: (13,5)

But it should be (16,6) for both c and e.
Any suggestions?
Thanks.


On 22 jul, 16:20, "Jim Langston" <(E-Mail Removed)> wrote:
> "clicwar" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...
>
>
>
> > A simple program with operator overloading and copy constructor:
> > #include <iostream>
> > #include <string>
> > using namespace std;

>
> > class Vector {
> > private:
> > float x,y;
> > public:
> > Vector(float u, float v);
> > Vector(void);
> > Vector operator+ (Vector a);
> > Vector(Vector &source);
> > void Show(void);
> > };
> > void Vector::Show(void) {
> > cout <<"(" << x <<"," <<y <<")";
> > }
> > Vector::Vector(float u, float v) {
> > x=u; y=v;
> > }
> > Vector::Vector(void) {
> > x=0; y=0;
> > }
> > Vector::Vector(Vector &source) {
> > x = (source.x)*2 ; y = (source.y)*2 ;
> > }
> > Vector Vector:perator+ (Vector a) {
> > Vector temp;
> > temp.x = x + a.x;
> > temp.y = y + a.y;
> > return temp;
> > }

>
> > int main(void) {
> > Vector a(3,1), b(5,2), c, d;
> > c = a+b;
> > d = a.operator+ (b);
> > cout << "Data members of the vector c: ";
> > c.Show();
> > Vector e(a+b);
> > cout <<endl << "Data members of the vector e: ";
> > e.Show();

>
> > return 0;
> > }

>
> > The compiler (g++ -pedantic -W -Wall) says:
> > teste.cpp: In function `int main()':
> > teste.cpp:36: error: no matching function for call to
> > `Vector::Vector(Vector)'
> > teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
> > teste.cpp:37: error: no matching function for call to
> > `Vector::Vector(Vector)'
> > teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
> > teste.cpp:40: error: no matching function for call to
> > `Vector::Vector(Vector)'
> > teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
> > teste.cpp:21: note: Vector::Vector()
> > teste.cpp:18: note: Vector::Vector(float, float)

>
> > Without the copy constructor Vector::Vector(Vector &source) , it works
> > fine.

>
> > Would anyone know what is wrong in the code?
> > Thanks in advance.

>
> Change it to:
> Vector::Vector( const Vector& a )
> and it should work.



 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      07-22-2007
"clicwar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> First of all, thank you Jim. It worked ( but i don't know why the
> simple addition of the const modifier can do that. Could you tell
> me? ).
>
> The output now is:
> Data members of the vector c: (13,5)
> Data members of the vector e: (13,5)
>
> But it should be (16,6) for both c and e.
> Any suggestions?
> Thanks.
>
>
> On 22 jul, 16:20, "Jim Langston" <(E-Mail Removed)> wrote:
>> "clicwar" <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed) ups.com...
>>
>>
>>
>> > A simple program with operator overloading and copy constructor:
>> > #include <iostream>
>> > #include <string>
>> > using namespace std;

>>
>> > class Vector {
>> > private:
>> > float x,y;
>> > public:
>> > Vector(float u, float v);
>> > Vector(void);
>> > Vector operator+ (Vector a);
>> > Vector(Vector &source);
>> > void Show(void);
>> > };
>> > void Vector::Show(void) {
>> > cout <<"(" << x <<"," <<y <<")";
>> > }
>> > Vector::Vector(float u, float v) {
>> > x=u; y=v;
>> > }
>> > Vector::Vector(void) {
>> > x=0; y=0;
>> > }
>> > Vector::Vector(Vector &source) {
>> > x = (source.x)*2 ; y = (source.y)*2 ;
>> > }
>> > Vector Vector:perator+ (Vector a) {
>> > Vector temp;
>> > temp.x = x + a.x;
>> > temp.y = y + a.y;
>> > return temp;
>> > }

>>
>> > int main(void) {
>> > Vector a(3,1), b(5,2), c, d;
>> > c = a+b;
>> > d = a.operator+ (b);
>> > cout << "Data members of the vector c: ";
>> > c.Show();
>> > Vector e(a+b);
>> > cout <<endl << "Data members of the vector e: ";
>> > e.Show();

>>
>> > return 0;
>> > }

>>
>> > The compiler (g++ -pedantic -W -Wall) says:
>> > teste.cpp: In function `int main()':
>> > teste.cpp:36: error: no matching function for call to
>> > `Vector::Vector(Vector)'
>> > teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
>> > teste.cpp:37: error: no matching function for call to
>> > `Vector::Vector(Vector)'
>> > teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
>> > teste.cpp:40: error: no matching function for call to
>> > `Vector::Vector(Vector)'
>> > teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
>> > teste.cpp:21: note: Vector::Vector()
>> > teste.cpp:18: note: Vector::Vector(float, float)

>>
>> > Without the copy constructor Vector::Vector(Vector &source) , it works
>> > fine.

>>
>> > Would anyone know what is wrong in the code?
>> > Thanks in advance.

>>
>> Change it to:
>> Vector::Vector( const Vector& a )
>> and it should work.


Well, I get even a weirder result with your code (26.3 or something). Not
sure why, but if you fix your operator+ it'll show the correct answer of
16.6

Vector operator+ (const Vector& a) const;

Operators and constructors have specific signatures. const is part of them
and I believe references are also. I really don't know what was going on
that was giving us the wrong results (I.E. no clue what the compiler was
donig) and don't really care, cause fixing the operator+ fixes the problem.

As to why it didn't work without your constructor having const, that's
because it didn't recognize it as a copy constructor.

Which makes me think, changing it from a copy to a reference probably means
your copy constructor is not working right, let me look at it...
Your copy constructor is mulitplying the values by 2? That's not what a copy
constructor is supposed to do.

Incidently, you can merge your 2 constructors into one.

Vector(float u, float v);

Vector::Vector(float u = 0, float v = 0): x(u), y(v)
{
}

Also, this is C++. We don't put (void) when there are no parameters to a
method/function. We just use () I.E.
void Show();


 
Reply With Quote
 
I V
Guest
Posts: n/a
 
      07-22-2007
On Sun, 22 Jul 2007 19:11:46 +0000, clicwar wrote:
> A simple program with operator overloading and copy constructor:

[...]
> Vector::Vector(Vector &source) {
> x = (source.x)*2 ; y = (source.y)*2 ;
> }


Here, your "copy constructor" doesn't make an actual copy. Are you sure you
want to do that? It seems to me to be likely to cause confusion. Indeed, I
think this is why you're not getting the result you expect:

Vector a(3,1), b(5,2), c, d;
c = a+b;

Now, this calls a.operator+(b), which invokes the copy constructor on b;
so a.operator+ gets passed (10, 4); it then adds this to (3, 1), giving
(13, 5), and returns this, which itself invokes the copy constructor,
giving (26, 10). However, I think this last use of the copy constructor,
in the return, is optional; so the results of calling operator+ depend on
whether or not the compiler decides to optimize out the copy constructor.
This doesn't seem like a very good state of affairs.
 
Reply With Quote
 
clicwar
Guest
Posts: n/a
 
      07-22-2007
Jim, did you mean something like this?

#include <iostream>
#include <string>
using namespace std;

class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector();
Vector operator+ (const Vector &a) const;
Vector(const Vector &source);
void Show();
};
void Vector::Show() {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector(float u, float v) {
x=u; y=v;
}
Vector::Vector() {
x=0; y=0;
}
Vector::Vector(const Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector:perator+ (const Vector &a) const {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return (temp);
}

int main() {
Vector a(3,1), b(5,2), c, d;
c = (a+b);
d = (a.operator+ (b));
cout << "Data members of the vector c: ";
c.Show();
Vector e((a+b));
cout <<endl << "Data members of the vector e: ";
e.Show();

return 0;
}

Did you use g++ ? Because now i'm getting (8,3) for both c and e.

Thanks for the patience and for the (void) tip.

> Here, your "copy constructor" doesn't make an actual copy. Are you sure you
> want to do that?

IV, how can i improve this? I came from a little C background and i
just learned how to do operator overloading.


On 22 jul, 18:59, I V <(E-Mail Removed)> wrote:
> On Sun, 22 Jul 2007 19:11:46 +0000, clicwar wrote:
> > A simple program with operator overloading and copy constructor:

> [...]
> > Vector::Vector(Vector &source) {
> > x = (source.x)*2 ; y = (source.y)*2 ;
> > }

>
> Here, your "copy constructor" doesn't make an actual copy. Are you sure you
> want to do that? It seems to me to be likely to cause confusion. Indeed, I
> think this is why you're not getting the result you expect:
>
> Vector a(3,1), b(5,2), c, d;
> c = a+b;
>
> Now, this calls a.operator+(b), which invokes the copy constructor on b;
> so a.operator+ gets passed (10, 4); it then adds this to (3, 1), giving
> (13, 5), and returns this, which itself invokes the copy constructor,
> giving (26, 10). However, I think this last use of the copy constructor,
> in the return, is optional; so the results of calling operator+ depend on
> whether or not the compiler decides to optimize out the copy constructor.
> This doesn't seem like a very good state of affairs.



 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      07-23-2007

clicwar <(E-Mail Removed)> wrote in message...
> Jim, did you mean something like this?
>
> #include <iostream>
> #include <string>
> using namespace std;
>
> class Vector { private:
> float x,y;
> public:
> Vector(float u, float v);
> Vector();
> Vector operator+ (const Vector &a) const;
> Vector(const Vector &source);
> void Show();
> };


> void Vector::Show() {
> cout <<"(" << x <<"," <<y <<")";
> }
> Vector::Vector(float u, float v) {
> x=u; y=v;
> }


Doesn't look like you know about 'initialization lists'. Try this:

Vector::Vector( float u, float v ) : x(u), y(v) {} // note the colon

> Vector::Vector(){ x=0; y=0; }


Vector::Vector() : x(0), y(0){}

> Vector::Vector(const Vector &source) {
> x = (source.x)*2 ; y = (source.y)*2 ;
> }


Vector::Vector( Vector const &source) :
x( (source.x)*2 ), y( (source.y)*2 ){}

> Vector Vector:perator+ (const Vector &a) const {
> Vector temp;
> temp.x = x + a.x;
> temp.y = y + a.y;
> return (temp);
> }
>
> int main() {
> Vector a(3,1), b(5,2), c, d;
> c = (a+b);
> d = (a.operator+ (b));
> cout << "Data members of the vector c: ";
> c.Show();
> Vector e((a+b));
> cout <<endl << "Data members of the vector e: ";
> e.Show();
>
> return 0;
> }
>
> Did you use g++ ? Because now i'm getting (8,3) for both c and e.


GCC(MinGW)3.3.1 g++, I got:

// out: Data members of the vector c: (8.000000,3.000000)
// out: Data members of the vector e: (8.000000,3.000000)

>
> Thanks for the patience and for the (void) tip.
>


Please don't top-post.
--
Bob R
POVrookie


 
Reply With Quote
 
I V
Guest
Posts: n/a
 
      07-23-2007
On Sun, 22 Jul 2007 22:54:07 +0000, clicwar wrote:
>> Here, your "copy constructor" doesn't make an actual copy. Are you sure you
>> want to do that?

> IV, how can i improve this? I came from a little C background and i
> just learned how to do operator overloading.


The obvious copy constructor would be:

Vector::Vector(const Vector &source) {
x = source.x ; y = source.y;
}

(or use initialization lists, as BobR suggests, or just let the compiler
generate the copy-constructor).

This would clearly give you different results, namely (8, 3) rather than
(16, 6). I'm not sure exactly what you're trying to accomplish; why are you
multiplying the x and y elements of your vector by 2 when you make a copy?
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      07-23-2007
"clicwar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Jim, did you mean something like this?
>
> #include <iostream>
> #include <string>
> using namespace std;
>
> class Vector {
> private:
> float x,y;
> public:
> Vector(float u, float v);
> Vector();
> Vector operator+ (const Vector &a) const;
> Vector(const Vector &source);
> void Show();
> };
> void Vector::Show() {
> cout <<"(" << x <<"," <<y <<")";
> }
> Vector::Vector(float u, float v) {
> x=u; y=v;
> }
> Vector::Vector() {
> x=0; y=0;
> }
> Vector::Vector(const Vector &source) {
> x = (source.x)*2 ; y = (source.y)*2 ;
> }
> Vector Vector:perator+ (const Vector &a) const {
> Vector temp;
> temp.x = x + a.x;
> temp.y = y + a.y;
> return (temp);
> }
>
> int main() {
> Vector a(3,1), b(5,2), c, d;
> c = (a+b);
> d = (a.operator+ (b));
> cout << "Data members of the vector c: ";
> c.Show();
> Vector e((a+b));
> cout <<endl << "Data members of the vector e: ";
> e.Show();
>
> return 0;
> }
>
> Did you use g++ ? Because now i'm getting (8,3) for both c and e.
>
> Thanks for the patience and for the (void) tip.
>
>> Here, your "copy constructor" doesn't make an actual copy. Are you sure
>> you
>> want to do that?

> IV, how can i improve this? I came from a little C background and i
> just learned how to do operator overloading.
>
>
> On 22 jul, 18:59, I V <(E-Mail Removed)> wrote:
>> On Sun, 22 Jul 2007 19:11:46 +0000, clicwar wrote:
>> > A simple program with operator overloading and copy constructor:

>> [...]
>> > Vector::Vector(Vector &source) {
>> > x = (source.x)*2 ; y = (source.y)*2 ;
>> > }

>>
>> Here, your "copy constructor" doesn't make an actual copy. Are you sure
>> you
>> want to do that? It seems to me to be likely to cause confusion. Indeed,
>> I
>> think this is why you're not getting the result you expect:
>>
>> Vector a(3,1), b(5,2), c, d;
>> c = a+b;
>>
>> Now, this calls a.operator+(b), which invokes the copy constructor on b;
>> so a.operator+ gets passed (10, 4); it then adds this to (3, 1), giving
>> (13, 5), and returns this, which itself invokes the copy constructor,
>> giving (26, 10). However, I think this last use of the copy constructor,
>> in the return, is optional; so the results of calling operator+ depend on
>> whether or not the compiler decides to optimize out the copy constructor.
>> This doesn't seem like a very good state of affairs.


Here's your program cleaned up a bit and extened a little.

#include <iostream>
#include <string>

class Vector
{
private:
float x,y;
public:
Vector(float u, float v);
Vector operator+ (const Vector &a) const;
Vector operator* (const float a ) const;
Vector operator* (const Vector &b) const;
Vector(const Vector &source);
void Show();
};

void Vector::Show()
{
std::cout <<"(" << x <<"," <<y <<")";
}

Vector::Vector(float u = 0, float v = 0): x(u), y(v)
{
}


Vector::Vector(const Vector &source)
{
x = source.x;
y = source.y;
}

Vector Vector:perator+ (const Vector &a) const
{
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}

Vector Vector:perator* (float a) const
{
Vector temp;
temp.x = x * a;
temp.y = y * a;
return temp;
}

Vector Vector:perator* (const Vector& a ) const
{
Vector temp;
temp.x = x * a.x;
temp.y = y * a.y;
return temp;
}

int main()
{
Vector a(3,1), b(5,2), c, d;

c = a+b;
d = (a.operator+ (b));
std::cout << "Data members of the vector c: ";
c.Show();
Vector e((a+b));
std::cout << std::endl << "Data members of the vector e: ";
e.Show();
std::cout << "\na * 2 = ";
(a * 2).Show();
std::cout << "\na * b = ";
(a * b).Show();

return 0;
}

All a copy constructor should do is copy. x = x, y = y. Nothing more. If
you want to mulitply, have an operator*

The multiplication of the two Vectors (Vector * Vector) is arbitary. A lot
of times multiplying two vectors would return the dot product. It dpends on
what you want it to do.


 
Reply With Quote
 
clicwar
Guest
Posts: n/a
 
      07-23-2007
>All a copy constructor should do is copy. x = x, y = y. Nothing more. If
>you want to mulitply, have an operator*


Now i understand. I was looking to the copy operator as a way to do
whatever i want with the data members in the moment of the
instantiation of the object.
Now i know that the copy constructor just ... copy.
If i want an additional functionality , i must supply an additional
function outside the copy constructor.

At this moment the fact of implement my own copy constructors seems
very useless, since the compiler provides me a default implementation.
But i'm sure that in the future i will change my mind.


>Please don't top-post

Sorry, i don't know what is top-post. But, surely i will not do again
if you tell me what it is.

Thanks Jim,IV and BobR.

On 22 jul, 22:15, "Jim Langston" <(E-Mail Removed)> wrote:
> "clicwar" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...
>
>
>
> > Jim, did you mean something like this?

>
> > #include <iostream>
> > #include <string>
> > using namespace std;

>
> > class Vector {
> > private:
> > float x,y;
> > public:
> > Vector(float u, float v);
> > Vector();
> > Vector operator+ (const Vector &a) const;
> > Vector(const Vector &source);
> > void Show();
> > };
> > void Vector::Show() {
> > cout <<"(" << x <<"," <<y <<")";
> > }
> > Vector::Vector(float u, float v) {
> > x=u; y=v;
> > }
> > Vector::Vector() {
> > x=0; y=0;
> > }
> > Vector::Vector(const Vector &source) {
> > x = (source.x)*2 ; y = (source.y)*2 ;
> > }
> > Vector Vector:perator+ (const Vector &a) const {
> > Vector temp;
> > temp.x = x + a.x;
> > temp.y = y + a.y;
> > return (temp);
> > }

>
> > int main() {
> > Vector a(3,1), b(5,2), c, d;
> > c = (a+b);
> > d = (a.operator+ (b));
> > cout << "Data members of the vector c: ";
> > c.Show();
> > Vector e((a+b));
> > cout <<endl << "Data members of the vector e: ";
> > e.Show();

>
> > return 0;
> > }

>
> > Did you use g++ ? Because now i'm getting (8,3) for both c and e.

>
> > Thanks for the patience and for the (void) tip.

>
> >> Here, your "copy constructor" doesn't make an actual copy. Are you sure
> >> you
> >> want to do that?

> > IV, how can i improve this? I came from a little C background and i
> > just learned how to do operator overloading.

>
> > On 22 jul, 18:59, I V <(E-Mail Removed)> wrote:
> >> On Sun, 22 Jul 2007 19:11:46 +0000, clicwar wrote:
> >> > A simple program with operator overloading and copy constructor:
> >> [...]
> >> > Vector::Vector(Vector &source) {
> >> > x = (source.x)*2 ; y = (source.y)*2 ;
> >> > }

>
> >> Here, your "copy constructor" doesn't make an actual copy. Are you sure
> >> you
> >> want to do that? It seems to me to be likely to cause confusion. Indeed,
> >> I
> >> think this is why you're not getting the result you expect:

>
> >> Vector a(3,1), b(5,2), c, d;
> >> c = a+b;

>
> >> Now, this calls a.operator+(b), which invokes the copy constructor on b;
> >> so a.operator+ gets passed (10, 4); it then adds this to (3, 1), giving
> >> (13, 5), and returns this, which itself invokes the copy constructor,
> >> giving (26, 10). However, I think this last use of the copy constructor,
> >> in the return, is optional; so the results of calling operator+ depend on
> >> whether or not the compiler decides to optimize out the copy constructor.
> >> This doesn't seem like a very good state of affairs.

>
> Here's your program cleaned up a bit and extened a little.
>
> #include <iostream>
> #include <string>
>
> class Vector
> {
> private:
> float x,y;
> public:
> Vector(float u, float v);
> Vector operator+ (const Vector &a) const;
> Vector operator* (const float a ) const;
> Vector operator* (const Vector &b) const;
> Vector(const Vector &source);
> void Show();
>
> };
>
> void Vector::Show()
> {
> std::cout <<"(" << x <<"," <<y <<")";
>
> }
>
> Vector::Vector(float u = 0, float v = 0): x(u), y(v)
> {
>
> }
>
> Vector::Vector(const Vector &source)
> {
> x = source.x;
> y = source.y;
>
> }
>
> Vector Vector:perator+ (const Vector &a) const
> {
> Vector temp;
> temp.x = x + a.x;
> temp.y = y + a.y;
> return temp;
>
> }
>
> Vector Vector:perator* (float a) const
> {
> Vector temp;
> temp.x = x * a;
> temp.y = y * a;
> return temp;
>
> }
>
> Vector Vector:perator* (const Vector& a ) const
> {
> Vector temp;
> temp.x = x * a.x;
> temp.y = y * a.y;
> return temp;
>
> }
>
> int main()
> {
> Vector a(3,1), b(5,2), c, d;
>
> c = a+b;
> d = (a.operator+ (b));
> std::cout << "Data members of the vector c: ";
> c.Show();
> Vector e((a+b));
> std::cout << std::endl << "Data members of the vector e: ";
> e.Show();
> std::cout << "\na * 2 = ";
> (a * 2).Show();
> std::cout << "\na * b = ";
> (a * b).Show();
>
> return 0;
>
> }
>
> All a copy constructor should do is copy. x = x, y = y. Nothing more. If
> you want to mulitply, have an operator*
>
> The multiplication of the two Vectors (Vector * Vector) is arbitary. A lot
> of times multiplying two vectors would return the dot product. It dpends on
> what you want it to do.



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
confusion with copy constructor and operator overloading Kelly Mandrake C++ 7 02-09-2005 08:38 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments