Reputation: 31
I have created a Vector3 class:
class Vector3{
public:
float x, y, z;
};
I can do this:
Vector3 v;
v.x = 0.0f;
v.y = 0.0f;
v.z = 0.0f
I want to initialize it like this:
Vector3 v = new Vector3(0.0f, 0.0f, 0.0f);
Upvotes: 0
Views: 94
Reputation: 122133
I suppose you are asking how to write a constructor that initializes the members:
class Vector3{
public:
float x, y, z;
Vector3(float a,float b, float c) : x(a),y(b),z(c) {}
Vector3() : x(0.0f), y(0.0f), z(0.0f) {}
};
Now you can write
Vector3 v(0.0f, 0.0f, 0.0f);
Vector3 v2;
Do not use new
to create objects. If you really need a dynamically allocated object, then use a smart pointer.
Upvotes: 1