# why the cctor is being activated ?

Page 1 of 1

## 10 Replies - 1046 Views - Last Post: 18 June 2011 - 02:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=235700&amp;s=2648d86a27728d18d9e79d5a870eea80&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 passer_by

Reputation: 5
• Posts: 234
• Joined: 06-March 11

# why the cctor is being activated ?

Posted 14 June 2011 - 07:24 PM

Hi guys ,

I have a little question regarding copy constructor .
In the following code :

// Point.h
#ifndef _POINT_
#define _POINT_
#include <iostream>
using namespace std;
class Point
{
private:
int m_iX, m_iY;
public:
Point (int valX, int valY);
~Point (); //destructor
void Show ();
int GetX() { return m_iX; }
int GetY() { return m_iY; }
//Distance between 2 points.
double Distance(Point p1);
};
#endif

// Point.cpp
#include "point.h"
#include <math.h>
// Calculates the distance between the
// two points.
double Point::Distance(Point p1) {
return (sqrt( pow(m_iX-p1.m_iX,2) + pow(m_iY-p1.m_iY,2)));
}
void Point::Show (){
cout<<"x="<<m_iX<<" y="<<m_iY<<endl;
}
Point::Point(int valX, int valY)
: m_iX(valX), m_iY(valY) {}
Point::~Point()  {// destructor
cout << "GoodBye:";
Show();
}

// Circle.h

#ifndef _CIRCLE_
#define _CIRCLE_

#include "point.h"

class Circle {
private:
Point  m_Center;

public:
Circle(); // Default constructor
// Initilization list constructor
~Circle() { // Destructor
cout << "circle destructor"<<endl;
Print();
}
Point MoveCenter (const Point& NewCenter);
bool   IsInside        (const Point& Location    );
bool   IsOverlap     (const Circle& NewCircle);
void Print();
};
#endif

// Circle.cpp
#include "circle.h"
#include <math.h>
Circle::Circle() // Default constructor
: m_Center(0,0), m_iRadius(1) {
cout <<"Circle default constructor\n“;
Print();
}
Circle::Circle(Point centerPoint, int iRadius) : m_Center(centerPoint),
cout << "Circle init list constructor\n“;
Print();
}
Point Circle::MoveCenter(const Point& newCntr) {
Point lastCenter=m_Center;
m_Center= newCntr;
return lastCenter;
}

bool Circle::IsInside(const Point& Location) {
double dDistance =
m_Center.Distance(Location);
}
bool Circle::IsOverlap(const Circle & NewCircle) {
double dDistance =
m_Center.Distance(NewCircle.m_Center);

return ( dDistance < iRadiusLen );
}
void Circle::Print() {
cout << "center:";
m_Center.Show();
cout << "radius:" << m_iRadius << endl;
}

int main() {

Point p1(12,12);

Circle c1;

Circle c2(p1,5);

Point p2(4,3);

cout <<c1.IsInside(p2)<<endl;        // when I run that line the cctor is being activated

...
return 0;
}

Now ,when I run "cout <<c1.IsInside(p2)<<endl;" in the main I get the
output of "GoodBye:x=4 y=3" . How can that be , if the method isInside (of the Circle class)
gets a pointer by const reference ...? why the Cctor is still being activated ? from the little I know about copy constructor , when I pass an object by reference , the cctor won't be used . Am I wrong ?

thanks ,Ron

This post has been edited by passer_by: 14 June 2011 - 07:27 PM

Is This A Good Question/Topic? 0

## Replies To: why the cctor is being activated ?

### #2 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: why the cctor is being activated ?

Posted 14 June 2011 - 07:39 PM

m_Center.Distance(Location);

double Point::Distance(Point p1)
is not being passed by reference so it will call the copy constructor.

This post has been edited by ishkabible: 14 June 2011 - 07:43 PM

### #3 passer_by

Reputation: 5
• Posts: 234
• Joined: 06-March 11

## Re: why the cctor is being activated ?

Posted 14 June 2011 - 07:53 PM

ishkabible, on 14 June 2011 - 07:39 PM, said:

m_Center.Distance(Location);

double Point::Distance(Point p1)
is not being passed by reference so it will call the copy constructor.

Maybe it's because Point class has no cctor ? then the compiler provides a cctor for Point ?

### #4 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: why the cctor is being activated ?

Posted 14 June 2011 - 08:08 PM

the text is printing from the destructor when the the instance of Point in Point::Distance goes out of scope.

Point::Distance has the prototype
double Point::Distance(Point p1)

which is not a pass by reference so, as mentioned above, the compiler is making a copy constructor which is being called.

This post has been edited by ishkabible: 14 June 2011 - 08:08 PM

### #5 passer_by

Reputation: 5
• Posts: 234
• Joined: 06-March 11

## Re: why the cctor is being activated ?

Posted 14 June 2011 - 08:23 PM

ishkabible, on 14 June 2011 - 08:08 PM, said:

the text is printing from the destructor when the the instance of Point in Point::Distance goes out of scope.

Point::Distance has the prototype
double Point::Distance(Point p1)

which is not a pass by reference so, as mentioned above, the compiler is making a copy constructor which is being called.

So what you're saying is that the reason for the cctor of Point is the "Point::Distance(Point p1)" ?
Okay , so :

1. If I would have done "Point::Distance(Point & p1)" , then the cctor of Point won't have been used ?
2. Does the Compiler provides a cctor when I don't have one (like a default Ctor , if the user doesn't
write one) ?

thanks ,Ron

This post has been edited by passer_by: 14 June 2011 - 08:25 PM

### #6 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: why the cctor is being activated ?

Posted 14 June 2011 - 08:42 PM

Quote

1. If I would have done "Point::Distance(Point & p1)" , then the cctor of Point won't have been used ?

yes

Quote

2. Does the Compiler provides a cctor when I don't have one (like a default Ctor , if the user doesn't

yes but you should always make a copy constructor, if the object is non-copyable then you make the copy constructor private.

### #7 passer_by

Reputation: 5
• Posts: 234
• Joined: 06-March 11

## Re: why the cctor is being activated ?

Posted 14 June 2011 - 09:00 PM

ishkabible, on 14 June 2011 - 08:42 PM, said:

Quote

2. Does the Compiler provides a cctor when I don't have one (like a default Ctor , if the user doesn't

yes but you should always make a copy constructor, if the object is non-copyable then you make the copy constructor private.

This is interesting , what would happen if I won't have a cctor of my own ? like in the code I posted above ?
Point doesn't have a cctor , then what are the implications of that ?
10x

### #8 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: why the cctor is being activated ?

Posted 14 June 2011 - 09:34 PM

Quote

This is interesting , what would happen if I won't have a cctor of my own ? like in the code I posted above ?

it would work the way you told it to.

Quote

Point doesn't have a cctor , then what are the implications of that ?

it means it calls the copy constructor of each member. this means you may get some unexpected/unwanted behavior if you have pointers in your class. however i strongly encourage you to make a default and copy constructor for every class you make.

This post has been edited by ishkabible: 14 June 2011 - 09:39 PM

### #9 passer_by

Reputation: 5
• Posts: 234
• Joined: 06-March 11

## Re: why the cctor is being activated ?

Posted 17 June 2011 - 04:14 AM

ishkabible, on 14 June 2011 - 09:34 PM, said:

Quote

This is interesting , what would happen if I won't have a cctor of my own ? like in the code I posted above ?

it would work the way you told it to.

Quote

Point doesn't have a cctor , then what are the implications of that ?

it means it calls the copy constructor of each member. this means you may get some unexpected/unwanted behavior if you have pointers in your class. however i strongly encourage you to make a default and copy constructor for every class you make.

Can you please give me an example where not having my own default ctor / default dtor and cctor , might
cause trouble to the program ? I think it has something to do with two pointers that points to the
same value .
Ron

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: why the cctor is being activated ?

Posted 17 June 2011 - 04:57 AM

### #11 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• Joined: 20-August 07

## Re: why the cctor is being activated ?

Posted 18 June 2011 - 02:46 AM

ishkabible, on 15 June 2011 - 05:34 AM, said:

however i strongly encourage you to make a default and copy constructor for every class you make.
I wouldn't encourage this at all. Most classes simply do not need it.

The only time you need a copy constructor is when your class has copy semantics which aren't satisfied by the default copy constructor (for example, if you're wrapping any objects which need their resources to be explicity managed); you should not provide copy constructors just for the sake of having one - its simply unnecessary and adds additional code maintenance load for something which the compiler will do for you.

Most of the time, there's absolutely no need to provide a copy constructor for your class - especially if you are being prudent in using the STL to help you manage resources; however, if you do need one, then you should also keep in mind that you probably also need a destructor and an overloaded assignment operator too, in order to make sure your class does not leak its resources, and behaves consistently when you copy it with the = operator.

You also need to consider exception safety when you're copying a class' internals using the assignment operator - i.e. if your attempt to copy the object throws an exception then you must make sure that you don't invalidate the destination object at the same time.(for example, by a failure to create an object with new)

If your class does not explicitly allocate/deallocate resources (files, memory, network connections, database objects, etc) then you probably do not need to worry aboutany of this. Your Point and Circle classes do not need copy constructors.

This post has been edited by Bench: 18 June 2011 - 02:50 AM