Avoid copying object in list initialization

Hi all, when I run this program, I get the following:

New point created.
Another point created from copy constructor.
Circle created.

Is there a way to avoid copying the object (without using a pointer in the Circle class constructor arguments).

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <iostream>

using namespace std;

class Point {
private:
	double m_x, m_y;

public:
	Point(double x, double y): m_x(x), m_y(y)
	{
		cout << "New point created." << endl;
	}

	Point(const Point& point): m_x(point.X()), m_y(point.Y())
	{
		cout << "Another point created from copy constructor." << endl;
	}

	//returns x
	double X() const
	{
		return m_x;
	}

	//returns y
	double Y() const
	{
		return m_y;
	}
};

class Circle
{
private:
	Point m_center;
	double m_radius;

public:
	//argument constructor
	Circle(const Point &center, const double& radius) : m_center(center), m_radius(radius)
	{
		cout << "Circle created." << endl;
	}
};

int main()
{
	Point* p1 = new Point(1, 1);
	Circle* c1 = new Circle(*p1, 1);

	std::cin.get();
	return 0;
}
Last edited on
You create a Point object, and you then create a Circle object which has inside it a whole other Point object.

That's two independent Point objects in existence. Your code creates two Point objects, which is pretty much the minimum needed in order to have two independent Point objects.

Whether you create the second one by copying an existing one, or some other way, you're still going to have to create two of them.

Also, I'd be remiss if I didn't say that using new and delete is bad, and that there's no need at all for heap allocation in this code.

In modern C++<,instead of new and delete we have smart pointers and make_unique and make_shared, and this code doesn't need them anyway:

1
2
3
4
5
6
7
8
int main()
{
	Point p1(1, 1);
	Circle c1(p1, 1);

	std::cin.get();
	return 0;
}


Last edited on
yes, ┬┐but what if you don't need two points?Circle c(Point(1, 1), 1);
I was hoping that Circle(Point center, const double& radius) would construct just one point, namely c.m_menter
sadly, that doesn't work.

as an alternative, you may do Circle(double x, double y, double radius): m_center(x, y) but then the instantiation becomes less clear Circle c(1, 1, 1);


Point is not big, so the copy is not too expensive, but also it is not needed.
> Is there a way to avoid copying the object (without using a pointer in the Circle class constructor arguments).

Yes. Use a member initializer list for direct initialisation of the Point member object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

class Point {

    private:
        double m_x, m_y;

    public:

        Point(double x, double y): m_x(x), m_y(y)
        {
            std::cout << "New point created.\n" ;
        }

        Point(const Point& point): m_x(point.m_x), m_y(point.m_y)
        {
            std::cout << "Another point created from copy constructor.\n" ;
        }

        // ...
};

class Circle
{
    private:
        Point m_center;
        double m_radius;

    public:

        Circle(const Point &center, const double& radius) : m_center(center), m_radius(radius)
        {
            std::cout << "Circle created.\n" ;
        }

        Circle( double center_x, double center_y, double radius ) : m_center(center_x,center_y), m_radius(radius)
        {
            std::cout << "Circle created.\n" ;
        }
};

int main()
{
	Circle c1( 1, 1, 1 );
}
If you are intending to be able to move that point (for example, in a CAD package) and if you intend the circle to automatically follow the change of centre then there is some merit in m_center being a pointer, not a fixed variable.
Registered users can post here. Sign in or register to post.