Should I implement a structure with a constructor in the header or source file?

So, I am writing code with a structure [in C ++] and I am not sure whether to implement the structure in the header file or in the source file.

The structure includes a constructor:

struct Point
{
    double x;
    double y;

    Point(double xCo, double yCo)
    {
        this->x = xCo;
        this->y = yCo;
    }

    int comparePoint(Point point)
    {
        ...
    }
};

I wrote in the header file:

typedef struct Point point;

Is this enough or is it a bad design? When I read on some websites, the structure is usually implemented in the header file,

but in the previous assignment that I had [in C], course staff provided us with a header file that included the ad in the structure and NOT the implementation.

I saw here other questions similar to this, but they really did not answer my question.

+5
source share
4 answers

cpp:

struct Point
{
    Point(double xCo, double yCo);
    ...
};

Point::Point(double xCo, double yCo)
{
    ...
}

, cpp, .

+3

struct C ++.

C struct , . struct Point, typedef.

++ struct class, (private class, public struct). struct typedef, typedef struct ++ .

: . . , , args , , , cpp, .

+7

, . , , ? , . , ++ struct class , .

, :

Point(double xCo, double yCo) : x(xCo), y(yCo) {}
+5

Given that in C ++, a structure is just a class with "default element accessibility" set to public, not private, I suggest you do the same with your structure as if you were encoding a class.

+1
source

All Articles