Should I use structs in C++?

The difference between struct and class is small in C++, basically only that struct members are per default public and class members are per default private. However, I still use structs whenever I need pure data structures, for instance: struct Rectangle { int width; int height; }; I find that very convenient to work with: Rectangle r; r.width = 20; r.height = 10; However, data str

我应该使用C ++中的结构吗?

C ++中结构和类的区别很小,基本上只有那些结构成员是默认公共的,而类成员是默认私有的。 但是,每当我需要纯数据结构时,我仍然使用结构体,例如: struct Rectangle { int width; int height; }; 我发现与之合作非常方便: Rectangle r; r.width = 20; r.height = 10; 但是,数据结构来自程序编程,而我正在做面向对象编程。 将这个概念引入OO是不是一个好主意? 不。如果在某个地方使用结构是有意义的,那

If I delete a class, are its member variables automatically deleted?

I have been researching, and nothing relevant has come up, so I came here. I am trying to avoid memory leaks, so I am wondering: Say I have class MyClass with member int s a and b , and an int array c , which are filled in a member function: class MyClass { public: int a, b; int c[2]; void setVariables() { a, b = 0; for (int i = 0;

如果我删除了一个类,它的成员变量是否自动删除?

我一直在研究,没有什么相关的,所以我来到这里。 我试图避免内存泄漏,所以我想知道: 假设我有一个类MyClass带有成员int s a和b以及一个int array c ,它们被填充到一个成员函数中: class MyClass { public: int a, b; int c[2]; void setVariables() { a, b = 0; for (int i = 0; i < 2; i++) { c[i] = 3; }

Parsing a binary file. What is a modern way?

I have a binary file with some layout I know. For example let format be like this: 2 bytes (unsigned short) - length of a string 5 bytes (5 x chars) - the string - some id name 4 bytes (unsigned int) - a stride 24 bytes (6 x float - 2 strides of 3 floats each) - float data The file should look like (I added spaces for readability): 5 hello 3 0.0 0.1 0.2 -0.3 -0.4 -0.5 Here 5 - is 2 b

解析二进制文件。 什么是现代方式?

我有一个我知道的布局的二进制文件。 例如让格式如下所示: 2个字节(无符号短整数) - 一个字符串的长度 5个字节(5个字符) - 字符串 - 一些id名称 4个字节(无符号整数) - 一个步幅 24个字节(6个浮点数 - 每个浮点数2个浮点数) - 浮点数据 该文件应该看起来像(为了便于阅读,我添加了空格): 5 hello 3 0.0 0.1 0.2 -0.3 -0.4 -0.5 这里5 - 是2个字节:0x05 0x00。 “你好” - 5个字节等。 现在我想读这

Why can't you use offsetof on non

I was researching how to get the memory offset of a member to a class in C++ and came across this on wikipedia: In C++ code, you can not use offsetof to access members of structures or classes that are not Plain Old Data Structures. I tried it out and it seems to work fine. class Foo { private: int z; int func() {cout << "this is just filler" << endl; return 0;} public:

为什么你不能使用offsetof on non

我正在研究如何在C ++中获得成员的内存偏移量,并在wikipedia上遇到这个问题: 在C ++代码中,您不能使用offsetof来访问不是普通旧数据结构的结构或类的成员。 我试了一下,它似乎工作正常。 class Foo { private: int z; int func() {cout << "this is just filler" << endl; return 0;} public: int x; int y; Foo* f; bool returnTrue() { return false; } }; int main() {

Why should I prefer to use member initialization list?

I'm partial to using member initialization lists with my constructors... but I've long since forgotten the reasons behind this... Do you use member initialization lists in your constructors? If so, why? If not, why not? For POD class members, it makes no difference, it's just a matter of style. For class members which are classes, then it avoids an unnecessary call to a default

为什么我更喜欢使用成员初始化列表?

我偏爱使用构造函数使用成员初始化列表...但是我早已忘记了背后的原因...... 你在构造函数中使用成员初始化列表吗? 如果是这样,为什么? 如果不是,为什么不呢? 对于POD类的成员来说,这没什么区别,只是风格问题。 对于类成员来说,它避免了对默认构造函数的不必要的调用。 考虑: class A { public: A() { x = 0; } A(int x_) { x = x_; } int x; }; class B { public: B() { a.x = 3

struct vs. class

Possible Duplicates: C/C++ Struct vs Class What are POD types in C++? Hi, In the C++ In a Nutshell book , in chapter 6: classes , unders Access specifiers , mentioned the following: In a class definition, the default access for members and base classes is private. In a struct definition, the default is public. That is the only difference between a class and a struct , although by conv

结构与类

可能重复: C / C ++结构与类 C ++中的POD类型是什么? 嗨, 在C ++ In a Nutshell的书中 ,在第6章:类中 ,在Access的说明符下 ,提到了以下内容: 在类定义中,成员和基类的默认访问是私有的。 在结构体定义中,默认是公共的。 这是一个类和一个结构之间的唯一区别 ,尽管按照惯例,一些程序员只对POD类使用结构,对所有其他类使用类 。 我在这里的问题是: 类和结构之间没有另外的区别,因为结构不包含函数

ness" in c++/c++11?

I have some code which takes a packed POD structure/class and copies it into a memory block. struct A { int a; int b; } a; memcpy(mymemoryblock, (void *)&a, sizeof(A)); // later I get a reply and... memcpy((void *)&a, mymemoryblock, sizeof(A)); This is only valid for POD types of data, and what I would like to know if there is a way I can test for POD-ness. If someone accident

ness“in c ++ / c ++ 11?

我有一些代码需要打包POD结构/类并将其复制到内存块中。 struct A { int a; int b; } a; memcpy(mymemoryblock, (void *)&a, sizeof(A)); // later I get a reply and... memcpy((void *)&a, mymemoryblock, sizeof(A)); 这仅适用于POD类型的数据,我想知道是否有方法可以测试POD性能。 如果有人不小心向该类中添加了成员​​函数,则memcpy操作将失效,但仍然可以编译。 这导致很难检测到错误。 有没有is_

lvalue and rvalue for pre/postfix increment

Learning the lvalue and rvalue. The definition is whatever that can be "address of" is the left value and otherwise, it is rvalue. I checked the operator precedence, both prefix and postfix increment has higher priority than the "address of" operator. For the following two examples, can anyone explain a bit why the first one "&++value1" is a lvalue while the

前/后缀增量的左值和右值

学习左值和右值。 定义是什么可以是“地址”是左值,否则,它是右值。 我检查了运算符优先级,前缀和后缀增量比“运算符的地址”具有更高的优先级。 对于以下两个例子,任何人都可以解释一下为什么第一个“&++ value1”是一个左值,而第二个“&value1 ++”是右值。 对这两种情况我的错误理解是:pValue1指向value1变量。 在建立地址关联之前或之后,无论value1变为8,value1变量总是占用一个内存位置,我们可以得到它的地址,

Can't make a function accept both rvalue and lvalue references

class Vec3{ private: float x, y, z; public: Vec3() = default; Vec3(const float c) {x = c; y = c; z = c;} static Vec3& normalize(Vec3& v) {/* normalize */ return v;} }; Vec3 aaa = Vec3( 1.0f); Vec3 bbb = Vec3::normalize( Vec3( 1.0f)); Vec3 ccc = Vec3::normalize( aaa); I wanted to write functions that take vectors as parameters, do some work on them and return them as

无法使函数接受右值和左值引用

class Vec3{ private: float x, y, z; public: Vec3() = default; Vec3(const float c) {x = c; y = c; z = c;} static Vec3& normalize(Vec3& v) {/* normalize */ return v;} }; Vec3 aaa = Vec3( 1.0f); Vec3 bbb = Vec3::normalize( Vec3( 1.0f)); Vec3 ccc = Vec3::normalize( aaa); 我想编写以矢量为参数的函数,对它们做一些处理并将它们作为参考返回。 在上面的代码中, bbb不会编译,

C++ Operator Overloading [ ] for lvalue and rvalue

I made a class Array which holds an integer array. From the main function, I'm trying to get an element of the array in Array using [ ] as we do for arrays declared in main. I overloaded the operator [ ] as in the following code; the first function returns an lvalue and the second an rvalue (Constructors and other member functions are not shown.) #include <iostream> using namespace

C ++运算符重载[]用于左值和右值

我做了一个包含整数数组的类Array。 从主函数中,我试图在Array中使用[]获取数组元素,就像我们在main中声明的数组一样。 我像下面的代码那样重载了operator []; 第一个函数返回一个左值并且第二个右值(构造函数和其他成员函数未显示)。 #include <iostream> using namespace std; class Array { public: int& operator[] (const int index) { return a[index]; } int operator[] (const int