dimanche 16 juin 2019

Aggregated structure constructor

I have the following code:

#include <iostream>
using namespace std;

struct Point {
    double x,y;
    Point(double x=0, double y=0): x(x), y(y) {}
};

struct Triangle {
    Point A,B,C;
    Triangle(Point A=Point(), Point B=Point(), Point C=Point()): A(A), B(B), C(C) {}
    void output()
    {
        cout<<"A: "<<A.x<<";"<<A.y<<endl;
        cout<<"B: "<<B.x<<";"<<B.y<<endl;
        cout<<"C: "<<C.x<<";"<<C.y<<endl;
    }
};

int main() {
    Triangle t;
    t.output();
    return 0;
}

Everything works fine. My question is about the Triangle constructor with default parameters. Is this a correct way to initialize the members by calling the Point constructor like this Point A=Point() (in terms of efficiency and clean code)?

Aucun commentaire:

Enregistrer un commentaire