[英]In C++, what is the most efficient way in defining a very simple object
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]. 我已经编写了一个
point
类,这是非常必要的,但是对于hexFace
,实际上它很简单,它只是四个点标签的列表,即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. 所以我的问题是:定义这种
hexFace
对象最有效的方法是什么? Should I use struct, or I'd better go with a class or anything else? 我应该使用struct还是最好去上一堂课或其他? 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: 您问几个问题:
what is the most efficient way in defining such an hexFace object.
定义此类hexFace对象的最有效方法是什么。
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: 如果您限于C ++ 11之前的功能,则可以使用:
struct hexFace {
int labels_[4];
};
If you can use C++11 features, try: 如果可以使用C ++ 11功能,请尝试:
class surface
{
private:
std::vector<point> points_;
std::vector<std::array<int, 4>> hexFaces_;
}
Should I use struct, or I'd better go with a class or anything else?
我应该使用struct还是最好去上一堂课或其他?
struct
and class
are nearly synonymous. struct
和class
几乎是同义词。 Use whichever you think expresses your intent more clearly. 使用任何您认为更能表达您意图的东西。 As for "something else", try
std::array
至于“其他”,请尝试
std::array
can I defining another class in the current class in a nesting way?
如何以嵌套方式在当前类中定义另一个类?
Yes, you can. 是的你可以。 Try:
尝试:
class surface
{
private:
class hexFace { public: int lables[4]; };
vector<point> points_;
vector<hexFace> hexFaces_;
};
If I can, do I have to write its constructors within this file also?
如果可以,是否还必须在该文件中编写其构造函数?
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: 这是内联的写法:
class surface {
public:
class hexFace { public: hexFace() { std::cout << "inline constructor!\n" } };
}
Here is how to write it externally 这是如何在外部编写它
class surface {
public:
class hexFace {
public:
hexFace();
};
// in another file ...
surface::hexFace::hexFace() { std::cout << "extern constructor\n"; }
Does a struct need a constructor to initialize it?
结构是否需要构造函数对其进行初始化?
Neither a class
nor a struct
require a user-defined constructor, but both allow them. class
和struct
都不需要用户定义的构造函数,但都允许它们。
struct X {
X() { std::cout << "in struct constructor!\n"; }
};
class Y {
public:
Y() { std::cout << "in class constructor!\n"; }
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.