'Should I use a constructor when size is known
Is there any different between these two implementation
//SIZE is known before runtime
class A{
int* p1 = new int[SIZE];
}
class B{
int* p1;
B(){//Using constructor
p1 = new int[SIZE];
}
}
Solution 1:[1]
There is no practical difference between these two examples.
P.S. Avoid owning bare pointers. Simplest solution for safe use of dynamic arrays is std::vector.
P.P.S. Prefer using member initialisers (either a default member initialiser such as in the second example, or in the member initialisation list) to assigning uninitialised members when possible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
