Aggregation and Composition with Pointer Obj

Why do we use "*" here ?

Linea::Linea(const Linea & obj) : inicial(new Punto(*obj.inicial)), final(new Punto(*obj.final)){}

and here we dont ?

Linea::Linea(const Punto & inicial, const Punto & final) : inicial(new Punto(inicial)), final(new Punto(final)){}

PS: I understand how the constructors work and that we are using COPY CONST.

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
#include<iostream>
using namespace std;

class Punto
	{
	private:
		double x;
		double y;
	public:
		Punto(double x = 0.0, double y = 0.0);
		Punto(const Punto & obj);
		~Punto();
		Punto & operator=(const Punto & obj);
		void setx(double x);
		void sety(double y);
		double getx() const;
		double gety() const;
		double distancia(const Punto & obj) const;
		friend ostream & operator<<(ostream & cout, const Punto & obj);
		friend istream & operator>>(istream & cin, Punto & obj);
	};

	class Linea
	{
		Punto *inicial;
		Punto *final;
		Linea(double x1 = 0.0, double y1 = 0.0, double x2 = 4.0, double y2 = 4.0);
		Linea(const Punto & inicial, const Punto & final);
		Linea(const Linea & obj);
		Linea & operator=(const Linea & obj);
		const Punto & getinicial() const;
		const Punto & getfinal() const;
		void setinicial(const Punto & obj);
		void setfinal(const Punto & obj);
		~Linea();
	};
	Linea::Linea(double x1, double y1, double x2, double y2) : inicial(new Punto(y1, y1)), final(new Punto(x2,y2)){}
	Linea::Linea(const Linea & obj) : inicial(new Punto(*obj.inicial)), final(new Punto(*obj.final)){}
	Linea::Linea(const Punto & inicial, const Punto & final) : inicial(new Punto(inicial)), final(new Punto(final)){}

int main()
{

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




Last edited on
The copy constructor of Punto expects a reference.

obj.inicial is a pointer so that is why you have to dereference it using * to get a reference to the object, before passing it to the Punto copy constructor.

In the second constructor inicial is a reference so you can pass it to the Punto copy constructor without having to do anything extra.
Last edited on
I always thought that obj.inicial and inicial were the same thing ? Why obj.inicial is a pointer and inicial isn't?

Sorry for my ignorance.
Last edited on
obj has type const Linea& (reference to const Linea). So to know the type of obj.inicial you should look inside the Linea class and see that the member variable inicial (line 25) has type Punto* (pointer to Punto).

In the other constructor you have a parameter variable named inicial. You should not confuse this with the member variable of Linea with the same name. It's just that the programmer have chosen to use the same name for both but you could use different names, it doesn't matter. The type of the constructor parameter inicial is clearly spelled out in the constructor parameter list as const Punto& (reference to const Punto).
Peter87, thanks for your help, time and detailed explanation.
Topic archived. No new replies allowed.