Home > Mobile >  My Vec3 class initialises without coding for it. How?
My Vec3 class initialises without coding for it. How?

Time:09-23

I made a custom vector3 class. In the class, I did not made a code that initializes via curly braces. However the code runs, even without coding it.

This works:

Vec3 pos = {1,1,0};

but I never coded for curly braces. How ?????? How it works ????????

This is my vec3 class header

class Vec3
{
private:
    float* data;
public:
    //float* data;
    // constructors
    Vec3();
    Vec3(float x, float y, float z);
    Vec3(Vec3&);
    Vec3(Vec3&&);
    ~Vec3();
    // getters setters
    float getX();   float getY();   float getZ();
    void setX(float);   void setY(float); void setZ(float);

    float& operator[](int);

    // maths
    float magnitude();
    Vec3& operator=(Vec3&);
    Vec3& operator=(Vec3&&);
    Vec3 operator-();
    friend Vec3 operator (Vec3&, Vec3&);  friend Vec3 operator (Vec3&, Vec3&&);  friend Vec3 operator (Vec3&&, Vec3&);  friend Vec3 operator (Vec3&&, Vec3&&);
    friend Vec3 operator-(Vec3&, Vec3&);  friend Vec3 operator-(Vec3&, Vec3&&);  friend Vec3 operator-(Vec3&&, Vec3&);  friend Vec3 operator-(Vec3&&, Vec3&&);
    friend float operator*(Vec3&, Vec3&); friend float operator*(Vec3&, Vec3&&); friend float operator*(Vec3&&, Vec3&); friend float operator*(Vec3&&, Vec3&&);
    friend Vec3 operator%(Vec3&, Vec3&);  friend Vec3 operator%(Vec3&, Vec3&&); friend Vec3 operator%(Vec3&&, Vec3&);  friend Vec3 operator%(Vec3&&, Vec3&&); 
    friend Vec3 operator*(float, Vec3&);
    friend Vec3 operator*(float, Vec3&&);


    // logical
    friend bool operator==(Vec3&, Vec3&); friend bool operator==(Vec3&, Vec3&&); friend bool operator==(Vec3&&, Vec3&); friend bool operator==(Vec3&&, Vec3&&);
    // output
    friend std::ostream & operator << (std::ostream& out, const Vec3& v);

};

CodePudding user response:

Vec3(float x, float y, float z); does this for as you have defined it already in your class.

CodePudding user response:

The statement Vec3 pos = {1,1,0}; is a copy-list-initialisation. {1,1,0} is not (always) an array expression, absent of context it does not have a type.

This looks at the constructors of Vec3 and finds Vec3(float x, float y, float z); as the best match for those arguments.

  •  Tags:  
  • c
  • Related