By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

confusion with copy constructor and operator overloading

P: n/a
I've implemented a class with operator+ overloaded and I also provided
my own copy constructor; The problem is my copy constructor is being
called twise and I dont understand why. I believe the copy constructor
is being called when I add the two class objects. But if this is so,
then why because I have overloaded the + operator should I not get the
result from that and not from copy constructor?

I have the following:

int main(int argc, char *argv[])
{
CVector a(2,5);
CVector b(3,6);
CVector c;

c = a + b;

cout << "X: " << c.GetX() << " Y: " << c.GetY() << endl;

DisplayCopy(c);

system("PAUSE");
return EXIT_SUCCESS;
}

CVector CVector::operator+(CVector param)
{
CVector temp;

temp.SetX( x + param.GetX() );
temp.SetY( y + param.GetY() );

return temp;
}

CVector::CVector(const CVector &rhs)
{
cout << "Inside Copy Constructor..." << endl;
x = rhs.x + 3;
y = rhs.y + 3;
}

void DisplayCopy(CVector copy)
{
cout << "X: " << copy.GetX() << " Y: " << copy.GetY() << endl;
}

==================================================

class CVector
{
public:
CVector(int valueX = 0, int valueY = 0): x(valueX), y(valueY) {}
CVector(const CVector &rhs); // Copy Constructor
CVector operator+(CVector param);

int GetX() const { return x; }
int GetY() const { return y; }
void SetX(int valueX) { x = valueX; }
void SetY(int valueY) { y = valueY; }
protected:
int x, y;
};

void DisplayCopy(CVector copy);

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Kelly Mandrake" <at******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I've implemented a class with operator+ overloaded and I also provided
my own copy constructor; The problem is my copy constructor is being
called twise and I dont understand why. I believe the copy constructor
is being called when I add the two class objects. But if this is so,
then why because I have overloaded the + operator should I not get the
result from that and not from copy constructor?

I have the following:

int main(int argc, char *argv[])
{
CVector a(2,5);
CVector b(3,6);
CVector c;

c = a + b;

cout << "X: " << c.GetX() << " Y: " << c.GetY() << endl;

DisplayCopy(c);

system("PAUSE");
return EXIT_SUCCESS;
}

CVector CVector::operator+(CVector param)
{
CVector temp;

temp.SetX( x + param.GetX() );
temp.SetY( y + param.GetY() );

return temp;
}

CVector::CVector(const CVector &rhs)
{
cout << "Inside Copy Constructor..." << endl;
x = rhs.x + 3;
y = rhs.y + 3;
}

void DisplayCopy(CVector copy)
{
cout << "X: " << copy.GetX() << " Y: " << copy.GetY() << endl;
}

==================================================

class CVector
{
public:
CVector(int valueX = 0, int valueY = 0): x(valueX), y(valueY) {}
CVector(const CVector &rhs); // Copy Constructor
CVector operator+(CVector param);
You should pass the param by const reference.
For example:
CVector operator+(const CVector & param);
This will eliminate a copy as the parameter is passed.
int GetX() const { return x; }
int GetY() const { return y; }
void SetX(int valueX) { x = valueX; }
void SetY(int valueY) { y = valueY; }
protected:
int x, y;
};

void DisplayCopy(CVector copy);


That parameter could also be passed by const reference.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #2

P: n/a
Kelly Mandrake wrote:
I've implemented a class with operator+ overloaded
and I also provided my own copy constructor; The problem is [that]
my copy constructor is being called twice and I don't understand why.
You pass the operands by value instead of const reference.
I believe the copy constructor is being called
when I add the two class objects.
But if this is so, then why because I have overloaded the + operator
should I not get the result from that and not from copy constructor? cat main.cc #include <iostream>

class CVector {
public:
CVector(int valueX = 0, int valueY = 0):
x(valueX), y(valueY) { }
CVector(const CVector &rhs); // Copy Constructor
CVector operator+(const CVector& param) const;

int GetX() const { return x; }
int GetY() const { return y; }
void SetX(int valueX) { x = valueX; }
void SetY(int valueY) { y = valueY; }
protected:
int x, y;
};

inline
CVector CVector::operator+(const CVector& param) const {
return CVector(x + param.GetX(), y + param.GetY());
}

inline
CVector::CVector(const CVector &rhs) {
std::cout << "Inside Copy Constructor..." << std::endl;
x = rhs.x + 3;
y = rhs.y + 3;
}

inline
void DisplayCopy(const CVector& copy) {
std::cout << "X: " << copy.GetX()
<< " Y: " << copy.GetY() << std::endl;
}

int main(int argc, char *argv[]) {
CVector a(2, 5);
CVector b(3, 6);
CVector c = a + b;

std::cout << "X: " << c.GetX()
<< " Y: " << c.GetY() << std::endl;

DisplayCopy(c);

system("PAUSE");
return EXIT_SUCCESS;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

X: 5 Y: 11
X: 5 Y: 11
sh: line 1: PAUSE: command not found
Jul 23 '05 #3

P: n/a
I changed it now so that it takes a const reference and of cource it
works fine now. I can see it now that it calls copy constructor
because im passing by value but when I read my code over and over I
wasnt seeing this. Thanks for all the help everyone.

Jul 23 '05 #4

P: n/a
In reguards to Roberts post can I ask why it is you recoment moveing my
inline methods out of the class. Is this to do with a standard, please
explain because If it is a standard I will try to follow it from now
on.

Jul 23 '05 #5

P: n/a
Kelly Mandrake wrote:
In regards to Robert's post,
Can I ask why it is [that] you recommend
moving my inline methods out of the class.
Is this to do with a standard,
please explain because, if it is a standard,
I will try to follow it from now on.
I did *not* move any [inline] methods
out of your CVector class definition.
You *declared* CVector::operator+(CVector)
and CVector::CVector(const CVector&)
in your CVector class definition
then *defined* them outside of the CVector class definition.
These are lightweight functions which should be inline'd
but they *must* be defined as inline functions
*before* they are invoked (in function main(int, char**) in this case).

"Standard" practice is to place the class definition
along with any inline function definition in a header file:
cat CVector.h #ifndef GUARD_CVECTOR_H
#define GUARD_CVECTOR_H 1

#include <iostream>

class CVector {
public:
CVector(int valueX = 0, int valueY = 0):
x(valueX), y(valueY) { }
CVector(const CVector &rhs); // Copy Constructor
CVector operator+(const CVector& param) const;

int GetX() const { return x; }
int GetY() const { return y; }
void SetX(int valueX) { x = valueX; }
void SetY(int valueY) { y = valueY; }
protected:
int x, y;
};

inline
CVector CVector::operator+(const CVector& param) const {
return CVector(x + param.GetX(), y + param.GetY());
}

inline
CVector::CVector(const CVector &rhs) {
std::cout << "Inside Copy Constructor..." << std::endl;
x = rhs.x + 3;
y = rhs.y + 3;
}

#endif//GUARD_CVECTOR_H

then include it in each source file that references CVector:
cat main.cc #include "CVector.h"

inline
void DisplayCopy(const CVector& copy) {
std::cout << "X: " << copy.GetX()
<< " Y: " << copy.GetY() << std::endl;
}

int main(int argc, char *argv[]) {
CVector a(2, 5);
CVector b(3, 6);
CVector c = a + b;

std::cout << "X: " << c.GetX()
<< " Y: " << c.GetY() << std::endl;

DisplayCopy(c);

system("PAUSE");
return EXIT_SUCCESS;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

X: 5 Y: 11
X: 5 Y: 11
sh: line 1: PAUSE: command not found
Jul 23 '05 #6

P: n/a
Ok I see now what you mean that you simply added the keywords inline.
Ive been putting the definitions inside my class alot too. So these
are considered light weight because they only have a few lines, or is
there a rule of thumb to determine weather to make them inline or not.

Jul 23 '05 #7

P: n/a
I see, very intelegent. So realy I could make all my funbctions inline
and thus would speed up execution of my program but because it slows
down the compiler as it needs to copy and paste inline functions where
they are used, it would be usefull to use preprocessor comands as you
showed so that I can compile quickly while develping and then wehn its
time for a distribution to be made, i compile with all inline
functions.

I like this idea. I must take the time to learn the preprocessor
statements, ive been avoiding them but it seems they are realy usefull.
Thanks for all the help.

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.