10 Replies - 1046 Views - Last Post: 18 June 2011 - 02:46 AM Rate Topic: -----

#1 passer_by   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • 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;
int      m_iRadius;

public:
Circle(); // Default constructor
// Initilization list constructor
Circle(Point CenterPoint,int iRadius);
~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),  
      m_iRadius(iRadius)  {
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);
return (dDistance<m_iRadius);
}
bool Circle::IsOverlap(const Circle & NewCircle) {
  double dDistance =
m_Center.Distance(NewCircle.m_Center);

  int iRadiusLen =
  m_iRadius+NewCircle.m_iRadius;
  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   User is offline

  • spelling expret
  • member icon





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

Re: why the cctor is being activated ?

Posted 14 June 2011 - 07:39 PM

what about this?
 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

Was This Post Helpful? 1
  • +
  • -

#3 passer_by   User is offline

  • D.I.C Head

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

Re: why the cctor is being activated ?

Posted 14 June 2011 - 07:53 PM

View Postishkabible, on 14 June 2011 - 07:39 PM, said:

what about this?
 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 ?
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 passer_by   User is offline

  • D.I.C Head

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

Re: why the cctor is being activated ?

Posted 14 June 2011 - 08:23 PM

View Postishkabible, 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

Was This Post Helpful? 0
  • +
  • -

#6 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#7 passer_by   User is offline

  • D.I.C Head

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

Re: why the cctor is being activated ?

Posted 14 June 2011 - 09:00 PM

View Postishkabible, 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
Was This Post Helpful? 0
  • +
  • -

#8 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 passer_by   User is offline

  • D.I.C Head

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

Re: why the cctor is being activated ?

Posted 17 June 2011 - 04:14 AM

View Postishkabible, 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
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

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

Re: why the cctor is being activated ?

Posted 17 June 2011 - 04:57 AM

For your reading
Was This Post Helpful? 1
  • +
  • -

#11 Bench   User is offline

  • D.I.C Lover
  • member icon

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

Re: why the cctor is being activated ?

Posted 18 June 2011 - 02:46 AM

View Postishkabible, 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1