How to declare and initialize an objects inside another object

Hello to everyone,
I have two classes, and I need to define one of them into the other.

I did something like the code below, but it doesn't work, and I don't understan why.

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
class Points :
{
private:
   int x*;
   int y*;

public:
   Points(int size)
   {
      x = new int[size];
      y = new int[size]
   }

   ~Points
   {
      delete[] x;
      delete[] y;
   }
}

class Triangle :
{
public:
   Points p;

   Triangle()
   {
      p = Points(3);
   }

   ~Tringle
   {
   }

   void fun1();
   void fun2();
}


The error is inside the Tringle class and the compiler tells me:
"No default constructor exist for class Points"

I also tried to define the Triangle class in that way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Triangle :
{
public:
   Points p(3);

   Triangle()
   {
   }

   ~Tringle
   {
   }

   void fun1();
   void fun2();
}


But also this last doesn't work.
This time the compiler tells:
"expected parameter declaration"

Can you expalin me why it doesn't work and how can I fix the problem?
Last edited on
1
2
3
4
   Triangle()
   {
      p = Points(3);
   }
This would default-initialize p, and then overwrite it again through the p = Points(3) assignment.

You must use a member initialization if you don't have a default constructor:
1
2
3
Triangle()
: p(3)
{ }


Points p(3);Close, but you can't declare a variable in this way in the class.
You'd have to do something like: Points p = Points(3);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Points {
private:
	int* x {};
	int* y {};

public:
	Points(size_t size) : x(new int[size]), y(new int[size]) {}

	// These need to be provided if required as the default ones are not appropriate here
	Points(const Points&) = delete;
	Points& operator=(const Points&) = delete;

	~Points() {
	   delete[] x;
	   delete[] y;
	}
};

class Triangle {
public:
	Points p;

	Triangle() : p(3) {}
};

Last edited on
Thanks to all for the replyes.

Can I also write something like that?

1
2
3
4
5
6
7
8
9
class Triangle :
{
public:
   Points p = Points(3);

   Triangle()
   {
   }
}


Is the same as the other solution?

1
2
3
Triangle()
: p(3)
{ }


Can you better explain :
1
2
Points(const Points&) = delete;
Points& operator=(const Points&) = delete;


Why Function() = delete ?
What happens when I set a function equal to delete?

I'm learning c++ and likely I have a lot to learn.

1) Yes. p will be default initialised to Points(3). If p isn't initialised elsewhere in a constructor then this will be it's value.

2) = delete means that the specified function isn't available and that any use will cause a compiler error. For classes, the compiler generates default functions for copy/move constructor and copy/move operator= in some cases. Where these are generated they do a shallow copy (just copy the contents of the member variables). When dynamic memory is used, a deep copy (allocation of new memory and copying of the contents from old to new) is required. Using the default constructors will cause problems - as in this case. That's why I specified these to be =delete so that if they are used you'll get a compiler error until correct implementations are provided.
Ok, thank you for the explanation.
Topic archived. No new replies allowed.