Suppose I am defining a surface object
class surface
{
private:
vector<point> points_;
vector<hexFace> hexFaces_;
}
I have already written a point class, which is very necessary, but as for hexFace, actually it is very simple, it is just a list of four point labels, that is int[4]. And I don’t need to do any complex operation on it.
So my question is: what is the most efficient way in defining such an hexFace object. Should I use struct, or I’d better go with a class or anything else? What would you do? Thanks
And if I need to go with class, can I defining another class in the current class in a nesting way? If I can, do I have to write its constructors within this file also?
Does a struct need a constructor to initialize it?
You ask several questions:
Run-time efficiency will be about the same for any solution you choose. Lines-of-code efficiency, or maintainer-programmer-brain-power efficiency is probably more valuable.
If you are limited to pre-C++11 features, I’d use:
If you can use C++11 features, try:
structandclassare nearly synonymous. Use whichever you think expresses your intent more clearly. As for “something else”, trystd::arrayYes, you can. Try:
You may, or you may choose to write it elsewhere, or you may choose to omit the user-defined constructor altogether.
Here is how to write it inline:
Here is how to write it externally
Neither a
classnor astructrequire a user-defined constructor, but both allow them.