In your case you don't need a pointer
class Test {
private:
int q;
Coordinate point;
public:
Test(int a, int b, int c) : q(a), point(b, c) {};
};
int main() {
Test test(1, 2, 3);
return 0;
}
It would be enough.
If you want to allocate memory, I highly recommend using a smart pointer or container instead:
class Test {
private:
int q;
std::unique_ptr<Coordinate> point;
public:
Test(int a, int b, int c) : q(a), point(std::make_unique_ptr<Coordinate>(b, c)) {};
};
int main() {
auto test = std::make_unique<Test>(1, 2, 3);
return 0;
}
In both cases, you respect rule 3/5/0.
In the second case, you probably need to provide a copy constructor for your class:
Test(const Test& rhs) : q(rhs.q), point(std::make_unique<Coordinate>(*rhs.point)) {}
source
share