12

Ok so, I'm writing a code with a struct [in C++], and I'm not sure whether to implement the struct in the header file or in the source file.

The struct 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 it good enough, or is it a bad design? As I read in some websites, a struct is usually implemented in the header file,

but in a previous assignment that I had [in C] the course's staff provided us with a header file that included declaration to the struct and NOT the implementation.

I saw other questions here similar to this one, but they didn't really answer my question.

durron597
  • 31,968
  • 17
  • 99
  • 158
Nadin Haddad
  • 337
  • 2
  • 3
  • 9
  • 4
    That typedef is unnecessary in C++. It's a C idiom. – Antimony Sep 13 '12 at 14:15
  • It's no big deal. One difference is that if you put the implementation in the header file then any functions must be inline (they are inline by default in your sample code above). – john Sep 13 '12 at 14:29

4 Answers4

15

You can declare the constructor in the header and write down an implementation in the cpp file:

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

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

But there must be a declaration in the header as the compiler depends upon it for building the other cpp files that are dependent on the struct.

Willem van Ketwich
  • 5,666
  • 7
  • 49
  • 57
Pavel Ognev
  • 962
  • 7
  • 15
  • ok but, [this may sound stupid but..] how will I reach the "x" and "y" in the struct ? If I was writing a class, and the x,y were defined private I would add get/set functions in order to reach the x,y variables. Is it necessary to do the same here ? because if I don't, I get an error. – Nadin Haddad Sep 13 '12 at 14:51
  • @NadinHaddad: If your `x` and `y` data members are `public`, you don't need the get/set methods: you can just use dot notation (or `->` notation if you reference the structure/class instance via pointers). – Mr.C64 Sep 13 '12 at 14:54
12

There is a big difference between struct usage in C and C++.

In C, struct declaration declares a structure with a tag, not a type name. As the result, you must reference it by tag as struct Point unless you add a typedef around it.

In C++, struct is a synonym for class, different only in the default member accessibility (private for class, public for struct). A type name is always defined for the struct without a typedef, so adding a typedef around your struct in C++ is unnecessary.

This does not influence the way you declare your constructor: you can inline it in the header, or move it out to the implementation. Either way it is going to work. It is probably better to put simple initializers that passes args to the initialization list in the header, while more complex initialization should probably go in the cpp file, but it is entirely up to you.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
6

There is no answer to this, it really depends on the situation. Do you care that client code has access to the implementation of the constructor and that it will have to be recompiled every time you make a change there? If so, do not implement it in the header. Bear in mind that in this context, in C++ struct and class are equivalent, so the same general rules should apply to both.

What you should do is use the constructor initialization list:

Point(double xCo, double yCo) : x(xCo), y(yCo) {}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

Considering that in C++ a struct is just a class with "default member accessibility" set to public instead of private, I suggest you to do exactly the same with your struct as if you were coding a class.

Pascal Lécuyot
  • 526
  • 4
  • 10