472,328 Members | 1,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

confusion with copy constructor and operator overloading

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
7 2465
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: raj | last post by:
I need explanation of the behavior of the following code. When run as it is, it gives error. //-------------------------<start>------------------...
1
by: Bob | last post by:
Hi, I'm trying to use a map with a string key, and a pointer to objects contained in a vector. I've wrapped this in a class like so: // cMap...
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. ...
7
by: pauldepstein | last post by:
A book I have describes a class called vector. It then explains (and I understand) the concept of a copy constructor, which is needed to...
11
by: fourfires.d | last post by:
Dear All, I am new to c++ and when write below code that try to call copy constructor in "=" operator overloading, it can not compile. Can...
4
by: coder_lol | last post by:
Given the template class below, please help me understand the following code behavior? Array<int32ia(10); // Array that can contain 10 int32...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector {...
0
by: Donovan Parks | last post by:
Hello, I am rather lost about how to properly setup a copy constructors and operator= with my template class. My class if as follows: ...
4
by: hjast | last post by:
I'm trying to implement a = operater to set one circle or rectangle to another and this is giving me all kind of bugs. #include <iostream.h> ...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.