Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redundancy of structures

Tags:

c++

I'd like to get some opinion from more experienced programmers. I have a structure like:

struct Position {
    int x;
    int y;
};

but I need to store for example longitude in a structure like:

struct Longitude {
    int from;
    int to;
};

both of them are actually the same with different names, but x and y are misleading in the case of Longitude. Would you use some typedef Position Longitude instead of defining Longitude structure (but then we have x/y there...)? Or create the same redundant structure with another names? Or maybe there are other alternatives?

like image 252
tobi Avatar asked Dec 30 '13 10:12

tobi


1 Answers

I'd be inclined to keep them separate.

In C++ a struct and a class are identical constructs (excepting the default access of member variables and functions).

As your application evolves, you'll probably want to add more functions and member data to the structs. At that point the two definitions will start to diverge. Keeping them separate from the outset will assist this development.

If you're concerned about code duplication then you can always inherit from a base class or struct.

like image 162
Bathsheba Avatar answered Sep 29 '22 09:09

Bathsheba