在c++中,在哪些情况下使用结构体比使用类更好?
当前回答
类。
默认情况下,类成员是私有的。
class test_one {
int main_one();
};
等于
class test_one {
private:
int main_one();
};
所以如果你尝试
int two = one.main_one();
我们将得到一个错误:main_one是私有的,因为它不可访问。我们可以 通过指定它的公共ie来初始化它来解决它
class test_one {
public:
int main_one();
};
结构体。
struct是一个类,其成员默认为public。
struct test_one {
int main_one;
};
意味着main_one是私有的,即
class test_one {
public:
int main_one;
};
我用struct表示数据结构,其中成员可以取任何值 那样容易些。
其他回答
我从来不在c++中使用struct。
我无法想象在需要私有成员时使用结构体的场景,除非您故意试图混淆。
使用结构体似乎更像是一种如何使用数据的语法指示,但我宁愿只创建一个类,并尝试在类的名称中显式地表示它,或通过注释。
E.g.
class PublicInputData {
//data members
};
正如其他人指出的那样
除了默认可见性之外,两者都是等效的 无论出于什么原因,都可能有理由被迫使用其中一种或另一种
关于何时使用Stroustrup/Sutter给出了明确的建议:
如果类有不变量,则使用class;如果数据成员可以独立变化,则使用struct
然而,请记住,将某事物前向声明为类(class X;)并将其定义为struct (struct X{…})。 它可能在某些链接器上工作(例如g++),但可能在其他链接器上失败(例如MSVC),所以你会发现自己陷入了开发人员的地狱。
摘自c++ FAQ Lite:
The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults. struct and class are otherwise functionally equivalent. OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.
从技术上讲,这两者在c++中是相同的——例如,结构体可能具有重载操作符等。
然而:
当我希望同时传递多种类型的信息时,我使用结构体 当我处理一个“功能性”对象时,我使用类。
希望能有所帮助。
#include <string>
#include <map>
using namespace std;
struct student
{
int age;
string name;
map<string, int> grades
};
class ClassRoom
{
typedef map<string, student> student_map;
public :
student getStudentByName(string name) const
{ student_map::const_iterator m_it = students.find(name); return m_it->second; }
private :
student_map students;
};
例如,我在这里的get…()方法中返回一个struct student -喜欢。
当你用c++实现提供兼容C的接口时,struct(更一般地说是PODs)非常方便,因为它们可以跨语言边界和链接器格式移植。
如果您不关心这一点,那么我认为使用“struct”而不是“class”是一种很好的意图交流方式(如上面的@ZeroSignal所述)。struct还具有更可预测的复制语义,因此对于打算写入外部媒体或通过网络发送的数据非常有用。
struct对于各种元编程任务也很方便,比如trait模板只暴露了一堆依赖的typedef:
template <typename T> struct type_traits {
typedef T type;
typedef T::iterator_type iterator_type;
...
};
...但这实际上只是利用了struct的默认保护级别是公共的……
推荐文章
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- 接口方法的最终参数-有什么意义?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- 在Java中使用“final”修饰符
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?