Dynamically allocating an array of objects

This is kind of a beginners question, but I haven't done C++ in a long time, so here goes... I have a class that contains a dynamically allocated array, say class A { int* myArray; A() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB's helpful style critique, no need to check against 0.

动态分配一个对象数组

这是一个初学者的问题,但我很久没有做C ++了,所以这里... 我有一个包含动态分配数组的类,比如说 class A { int* myArray; A() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB's helpful style critique, no need to check against 0. delete [] myArray; } } 但是现在我想创建一个动态分配的这

What is copy elision and how does it optimize the copy

I was reading Copy and Swap. I tried reading some links on Copy Elision but could not figure out properly what it meant. Can somebody please explain what this optimization is, and especially what is mean by the following text This is not just a matter of convenience but in fact an optimization. If the parameter (s) binds to a lvalue (another non-const object), a copy of the object is made a

什么是复制省略以及它如何优化副本

我正在阅读复制和交换。 我尝试阅读Copy Elision上的一些链接,但无法正确理解它的含义。 有人可以解释一下这个优化是什么,特别是下面的文字是什么意思 这不仅仅是方便的问题,实际上是一种优化。 如果参数绑定到一个左值(另一个非const对象),则在创建参数时会自动创建该对象的副本。 但是,当s绑定到右值(临时对象,文字)时,副本通常会被省略,这会保存对复制构造函数和析构函数的调用。 在参数被接受为常量引用

Copy constructor and = operator overload in C++: is a common function possible?

Since a copy constructor MyClass(const MyClass&); and an = operator overload MyClass& operator = (const MyClass&); have pretty much the same code, the same parameter, and only differ on the return, is it possible to have a common function for them both to use? Yes. There are two common options. One - which I don't recommend - is to call the operator= from the copy construc

在C ++中复制构造函数和=运算符重载:是一个常用函数吗?

由于一个拷贝构造函数 MyClass(const MyClass&); 和an =运算符过载 MyClass& operator = (const MyClass&); 具有几乎相同的代码,相同的参数,并且仅在返回时有所不同,是否可以为它们使用共同的函数? 是。 有两个常用选项。 其中之一 - 我不建议 - 从显式拷贝构造函数中调用operator= : MyClass(const MyClass& other) { operator=(other); } 然而,提供一个好的operator=对于处理旧的状态和自

What are your favorite C++ Coding Style idioms

What are your favorite C++ coding style idioms? I'm asking about style or coding typography such as where you put curly braces, are there spaces after keywords, the size of indents, etc. This is opposed to best-practices or requirements such as always deleting arrays with delete[] . Here is an example of one of my favorites: In C++ Class initializers, we put the separators at the front of

你最喜欢的C ++编码风格成语是什么?

你最喜欢的C ++编码风格习语是什么? 我正在问风格或编码排版,比如放置大括号的位置,关键字后面是否有空格,缩进大小等等。这与最佳做法或要求相反,例如总是使用delete[]删除数组。 下面是我最喜欢的一个例子:在C ++类初始化程序中,我们把分隔符放在行的前面,而不是后面。 这可以更容易地保持最新状态。 这也意味着版本之间的源代码控制差异更为清晰。 TextFileProcessor:: TextFileProcessor( class ConstStringFind

How does this implementation of bitset::count() work?

Here's the implementation of std::bitset::count with MSVC 2010: size_t count() const { // count number of set bits static char _Bitsperhex[] = "112122312232334"; size_t _Val = 0; for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos) for (_Ty _Wordval = _Array[_Wpos]; _Wordval != 0; _Wordval >>= 4) _Val += _Bitsperhex[_Wordval & 0xF]; return (_Va

bitset :: count()的这个实现是如何工作的?

这是MSVC 2010中std::bitset::count的实现: size_t count() const { // count number of set bits static char _Bitsperhex[] = "112122312232334"; size_t _Val = 0; for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos) for (_Ty _Wordval = _Array[_Wpos]; _Wordval != 0; _Wordval >>= 4) _Val += _Bitsperhex[_Wordval & 0xF]; return (_Val); } 有人可以向我解

What does {0} mean when initializing an object?

When {0} is used to initialize an object, what does it mean? I can't find any references to {0} anywhere, and because of the curly braces Google searches are not helpful. Example code: SHELLEXECUTEINFO sexi = {0}; // what does this do? sexi.cbSize = sizeof(SHELLEXECUTEINFO); sexi.hwnd = NULL; sexi.fMask = SEE_MASK_NOCLOSEPROCESS; sexi.lpFile = lpFile.c_str(); sexi.lpParameters = args; sex

初始化对象时,{0}意味着什么?

当{0}用于初始化一个对象时,它意味着什么? 我无法在任何地方找到任何对{0}引用,并且由于Google搜索的大括号无效。 示例代码: SHELLEXECUTEINFO sexi = {0}; // what does this do? sexi.cbSize = sizeof(SHELLEXECUTEINFO); sexi.hwnd = NULL; sexi.fMask = SEE_MASK_NOCLOSEPROCESS; sexi.lpFile = lpFile.c_str(); sexi.lpParameters = args; sexi.nShow = nShow; if(ShellExecuteEx(&sexi)) { DWORD wait = W

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

为什么'sizeof'运算符返回的结构体积比结构体的总尺寸要大? This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs: Mis-aligned access might be a hard error (often SIGBUS ). Mis-aligned access might be a soft error. Either corrected in hardware, for a modest performance-degradation. Or correcte

为什么不是sizeof等于每个成员的sizeof之和?

为什么'sizeof'运算符返回的结构体积比结构体的总尺寸要大? 这是因为添加了填充以满足对齐约束。 数据结构调整会影响程序的性能和正确性: 错误对齐的访问可能是一个硬错误(通常是SIGBUS )。 错误对齐访问可能是一个软错误。 要么在硬件中纠正,要适度降低性能。 或者通过软件仿真来纠正,以降低性能。 另外,原子性和其他并发性保证可能会被破坏,从而导致微妙的错误。 以下是使用x86处理器的典型设置

What does int argc, char *argv[] mean?

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this: int main(int argc, char *argv[]) When I code C++ without an IDE, just with a command line compiler, I type: int main() without any parameters. What does this mean, and is it vital to my program? argv and argc are how command line arguments are passed to main() in C and C++. argc will be

int argc,char * argv []是什么意思?

在许多C ++ IDE和编译器中,当它为您生成主要功能时,它看起来像这样: int main(int argc, char *argv[]) 当我编写没有IDE的C ++时,只需使用命令行编译器,我键入: int main() 没有任何参数。 这是什么意思,对我的程序至关重要? argv和argc是命令行参数如何在C和C ++中传递给main() 。 argc将是argv指向的字符串的数量。 这将(实际上)为1加上参数的数量,因为实际上所有的实现都会在程序中加入数组名称。 按

Unary plus (+) against literal string

Today I wrote an expression: "<" + message_id + "@" + + ">" and got surprised that it actually compiled. (PS message_id is a QString, it would also work with an std::string) I often do things like that, leave out a variable as I'm working and I expect the compiler to tell me where I'm still missing entries. The final would look something like this: "<" + message_id + "@"

一元加(+)反对文字字符串

今天我写了一个表达: "<" + message_id + "@" + + ">" 并惊讶于它实际编译。 (PS message_id是一个QString,它也可以用于std :: string) 我经常这样做,在工作时忽略一个变量,我希望编译器告诉我我仍然缺少条目。 决赛看起来像这样: "<" + message_id + "@" + network_domain + ">" 现在我想知道为什么+一元运算符对字符串文本有效!? 一元+可以应用于算术类型值,非范围枚举值和指针值,因为...

Several unary operators in C and C++

Is it standard-conforming to use expressions like int i = 1; +-+-+i; and how the sign of i variable is determined? Yes it is. Unary + and - associate right-to-left, so the expression is parsed as +(-(+(-(+i)))); Which results in 1 . Note that these can be overloaded, so for a user-defined type the answer may differ. 你的操作符没有任何副作用, +i对int本身没有做任何事情,你不使用临时生成

C和C ++中的几个一元运算符

是否符合标准要使用像 int i = 1; +-+-+i; 以及我如何确定变量的符号? 是的。 一元+和-从右到左关联,因此表达式被解析为 +(-(+(-(+i)))); 其结果是1 。 请注意,这些可能会过载,因此对于用户定义的类型,答案可能会有所不同。 你的操作符没有任何副作用, +i对int本身没有做任何事情,你不使用临时生成的值,但删除+什么都不做,并且-(-i)女巫等于i自己。(删除代码中的+将转换运算符,我的意思是在计算中删除它,因