Static array vs. dynamic array in C++

What is the difference between a static array and a dynamic array in C++? I have to do an assignment for my class and it says not to use static arrays, only dynamic arrays. I've looked in the book and online, but I don't seem to understand. I thought static was created at compile time and dynamic at runtime, but I might be mistaking this with memory allocation. Can you explain the

静态数组与C ++中的动态数组

C ++中的静态数组和动态数组有什么区别? 我必须为我的课做一个任务,它说不使用静态数组,只使用动态数组。 我已经看过这本书和网上,但我似乎不明白。 我认为静态是在编译时创建的,在运行时是动态的,但我可能会误认为是内存分配。 你能解释C ++中静态数组和动态数组之间的区别吗? 本地数组在堆栈上创建,并具有自动存储持续时间 - 您不需要手动管理内存,但是当它们所在的函数结束时它们会被销毁。 他们必须有一

Why are move semantics necessary to elide temporary copies?

So my understanding of move semantics is that they allow you to override functions for use with temporary values (rvalues) and avoid potentially expensive copies (by moving the state from an unnamed temporary into your named lvalue). My question is why do we need special semantics for this? Why couldn't a C++98 compiler elide these copies, since it's the compiler that determines whethe

为什么需要移动语义来删除临时副本?

因此,我对移动语义的理解是,它们允许您覆盖函数以使用临时值(rvalues),并避免潜在的昂贵副本(通过将状态从未命名的临时移动到您的已命名的左值)。 我的问题是为什么我们需要特殊的语义? 为什么C ++ 98编译器不能使用这些副本,因为编译器决定给定的表达式是左值还是右值? 举个例子: void func(const std::string& s) { // Do something with s } int main() { func(std::string("abc") + std::string

Return value optimization and copy elision in C

Some people are not aware that it's possible to pass and return structs by value in C. My question is about the compiler making unnecessary copies when returning structs in C. Do C compilers such as GCC use Return value optimization(RVO) optimization or is this a C++ only concept? Everything I have read about RVO and copy elision is in regards to C++. Let's consider an example. I'

在C中返回值优化和复制elision

有些人没有意识到可以在C中传递和返回结构。我的问题是关于编译器在C中返回结构时做不必要的拷贝。像GCC这样的C编译器是使用返回值优化(RVO)优化还是这个一个C ++唯一的概念? 我读过的关于RVO和copy elision的一切都是关于C ++的。 我们来看一个例子。 我目前正在C中实现一个double-double数据类型(或者说float float,因为我发现它很容易进行单元测试)。 考虑下面的代码。 typedef struct { float hi; float

copy elision method

from the standard definition of copy elision method: In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects. let us consider following code #include <cstdlib> #include <iostream> using namespace std; int n=0; struct C { C (int) {} C(const C&) {++n;} }; int main(int argc, char *argv[]

复制elision方法

从copy elision方法的标准定义:在C ++计算机编程中,copy elision指的是一种编译器优化技术,可消除不必要的对象复制。 让我们考虑下面的代码 #include <cstdlib> #include <iostream> using namespace std; int n=0; struct C { C (int) {} C(const C&) {++n;} }; int main(int argc, char *argv[]) { C c1(42); C c2=42; return n; } 这行“返回n”将返回0或1,具体取决于副

What are copy elision and return value optimization?

What is copy elision? What is (named) return value optimization? What do they imply? In what situations can they occur? What are limitations? If you were referenced to this question, you're probably looking for the introduction . For a technical overview, see the standard reference . See common cases here. Introduction For a technical overview - skip to this answer. For commo

什么是复制省略和返回值优化?

什么是复制elision? 什么是(命名)返回值优化? 他们意味着什么? 在什么情况下可以发生? 什么是限制? 如果您参考了这个问题,您可能正在寻找介绍 。 有关技术概述,请参阅标准参考 。 在这里查看常见案例 。 介绍 技术概述 - 跳到这个答案。 对于出现复制瑕疵的常见情况 - 跳到此答案。 复制elision是由大多数编译器实施的优化措施,以防止在某些情况下额外(可能是昂贵的)副本。 它使价值回归或价值

>, .* (C++)

I understand most operator overloading, with the exception of the member access operators -> , .* , ->* etc. In particular, what is passed to these operator functions, and what should be returned? How does the operator function (eg operator->(...) ) know what member is being refered to? Can it know? Does it even need to know? Finally, are there any const considerations that need

>,。*(C ++)

我理解大多数运算符重载,除了成员访问运算符-> , .* , ->*等 特别是,传递给这些运算符函数的是什么,以及应该返回什么? 操作员的功能如何(例如, operator->(...) )知道被引用的成员? 它可以知道吗? 它甚至需要知道吗? 最后,是否有需要考虑的常量因素? 例如,当重载类似operator[]东西时,通常你需要一个const和非const的版本。 成员访问操作符是否需要const和非const版本? -> 这是唯一

Reducing code duplication between operator= and the copy constructor

I have a class that requires a non-default copy constructor and assignment operator (it contains lists of pointers). Is there any general way to reduce the code duplication between the copy constructor and the assignment operator? There's no "general way" for writing custom copy constructors and assignment operators that works in all cases. But there's an idiom called "

减少operator =和拷贝构造函数之间的代码重复

我有一个类需要一个非默认的拷贝构造函数和赋值操作符(它包含指针列表)。 有减少复制构造函数和赋值运算符之间的代码重复的一般方法吗? 在所有情况下编写自定义拷贝构造函数和赋值运算符都没有“通用方法”。 但有一个叫做“复制 - 交换”的成语: class myclass { ... public: myclass(myclass const&); void swap(myclass & with); myclass& operator=(myclass copy) { this->

C++ code in header files

My personal style with C++ has always to put class declarations in an include file, and definitions in a .cpp file, very much like stipulated in Loki's answer to C++ Header Files, Code Separation. Admittedly, part of the reason I like this style probably has to do with all the years I spent coding Modula-2 and Ada, both of which have a similar scheme with specification files and body files.

头文件中的C ++代码

我对C ++的个人风格总是将类声明放在包含文件中,并将定义放在.cpp文件中,这非常类似于Loki对C ++头文件代码分离的回答。 无可否认,我喜欢这种风格的部分原因可能与我花费在编码Modula-2和Ada上的所有年份有关,它们都与规范文件和正文文件有相似的方案。 我有一个同事,比C ++更懂C ++,他坚持所有的C ++声明都应该尽可能在头文件中包含这些定义。 他并不是说这是一种有效的替代风格,或者说是一种稍微好一点的风格,但是

C++ SFINAE examples?

I want to get into more template meta-programming. I know that SFINAE stands for "substitution failure is not an error." But can someone show me a good use for SFINAE? Heres one example (from here): template<typename T> class IsClassT { private: typedef char One; typedef struct { char a[2]; } Two; template<typename C> static One test(int C::*); // Will

C ++ SFINAE的例子?

我想进入更多的模板元编程。 我知道SFINAE代表“替代失败不是一个错误”。 但有人可以让我看到SFINAE的良好用途吗? 下面是一个例子(从这里): template<typename T> class IsClassT { private: typedef char One; typedef struct { char a[2]; } Two; template<typename C> static One test(int C::*); // Will be chosen if T is anything except a class. template<typename C>

Creating a new object from dynamic type info

In C++, is there any way to query the type of an object and then use that information to dynamically create a new object of the same type? For example, say I have a simple 3 class hierarchy: class Base class Foo : public Base class Bar : public Base Now suppose I give you an object cast as type Base -- which is in reality of type Foo. Is there a way to query the type and use that info to lat

从动态类型信息创建一个新对象

在C ++中,是否有任何方法可以查询对象的类型,然后使用该信息动态创建相同类型的新对象? 例如,假设我有一个简单的3类层次结构: class Base class Foo : public Base class Bar : public Base 现在假设我给你一个类型为Base的对象 - 这是Foo类型的实际。 有没有办法查询类型并使用该信息以稍后创建Foo类型的新对象? 克隆方法 查询类型的语言没有提供任何东西,并且可以根据这些信息构造,但是您可以通过各种方式为