473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2530
"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>------------------ #include <iostream.h> #include <stdlib.h> ...
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 template<class T> class cMap : public cList<T> { ...
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. The code in question is written below. The syntax...
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 interpret statements like vector b = a; But then I...
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 anyone point out for me the reason? thanks !! ...
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 ia = 1; // Array element 1 set to...
22
by: clicwar | last post by:
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...
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: template<class Tclass Image { private: IplImage* imgp;
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> class Shape { private: int x_Center,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.