C++11 rvalues and move semantics confusion (return statement)

I'm trying to understand rvalue references and move semantics of C++11. What is the difference between these examples, and which of them is going to do no vector copy? First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); Second example std::vector<int>

C ++ 11 rvalues和移动语义混淆(return语句)

我试图理解右值引用并移动C ++ 11的语义。 这些例子之间的区别是什么,它们中的哪一个将不做矢量拷贝? 第一个例子 std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); 第二个例子 std::vector<int>&& return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; r

c++

I'm a bit confused regarding the difference between push_back and emplace_back . void emplace_back(Type&& _Val); void push_back(const Type& _Val); void push_back(Type&& _Val); As there is a push_back overload taking a rvalue reference I don't quite see what the purpose of emplace_back becomes? In addition to what visitor said : The function void emplace_back(Type

C ++

对于push_back和emplace_back之间的区别我有点困惑。 void emplace_back(Type&& _Val); void push_back(const Type& _Val); void push_back(Type&& _Val); 由于有一个push_back超载需要右值引用,我不太明白emplace_back的目的是什么? 除了什么访问者说: 由MSCV10提供的函数void emplace_back(Type&& _Val)是不符合和冗余的,因为正如你所说的那样,它严格地等同于push_back(Type&&

Print Operator overload fail?

Am practicing C++ basic inheritance concepts, came across the need to print out a custom class object and wrote an overload, followed guide exactly yet still does not register use of the new operator for printing (<<). Am wondering if typed incorrectly or had some declaration/initiation errors somewhere else? no match for 'operator<<' (operand types are 'std::basic_ost

打印操作员超载失败?

我在练习C ++基本继承概念时,遇到了打印出一个自定义类对象并写入一个过载的需求,完全按照指导原则进行,但仍未注册使用新操作符进行打印(<<)。 我想知道是否输入错误或在其他地方有一些声明/启动错误? 'operator <<'没有匹配(操作数类型是'std :: basic_ostream'和'Choco')std :: cout <<“Choco value:”<< child << endl; #include <iostream> using

C++ template class and operator overloading

This question already has an answer here: What are the basic rules and idioms for operator overloading? 7 answers Your assignment operator is broken. First you call delete on internal , invalidating it. Then you call init , which assigns to the elements of internal . You need a reallocation between those two steps. On another note, your copy constructor is broken too. Set internal to n

C ++模板类和运算符重载

这个问题在这里已经有了答案: 运算符重载的基本规则和习惯用法是什么? 7个答案 您的分配操作员已损坏。 首先你打电话给internal删除,使其无效。 然后你调用init ,分配给internal元素。 您需要在这两个步骤之间重新分配。 另一方面,你的拷贝构造函数也被打破了。 首先将internal设置为nullptr 。 否则,赋值运算符将在单位化指针上调用delete。 另一方面,您的加法运算符已损坏。 它假定这两个数组的大小相同

increment and decrement operator overloading

Here is the problem I has been trying to solve: Define a class named PrimeNumber that stores a prime number. The default constructor should set the prime number to 1. Add another constructor that allows the caller to set the prime number. Also, add a function to get the prime number. Finally, overload the prefix and postfix ++ and -- operators so they return a PrimeNumber object that is the

递增和递减运算符重载

这是我一直试图解决的问题: 定义一个名为PrimeNumber的类来存储质数。 默认构造函数应该将素数设置为1.添加另一个允许调用者设置素数的构造函数。 另外,添加一个函数来获取素数。 最后,重载前缀和后缀++和-- operators以便它们返回PrimeNumber对象,该对象是下一个最大素数(用于++ )和下一个最小素数(用于-- )。 例如,如果对象的素数设置为13,则调用++应返回质数设置为17的PrimeNumber对象。为该类创建适当的测试

Operator "<<" overloading return type

Suppose there is a cPoint class. class cPoint { int x, y, z; }; I wanted to print all of three variables in a single statement. So, I overloaded operator << just like friend std::ostream& operator<< (std::ostream &cout, cPoint &p); std::ostream& operator<< (std::ostream &out, cPoint &p) { out << p.get_x() << " " << p.ge

运算符“<<”重载返回类型

假设有一个cPoint类。 class cPoint { int x, y, z; }; 我想在一个语句中打印所有三个变量。 所以,我重载了operator <<就像 friend std::ostream& operator<< (std::ostream &cout, cPoint &p); std::ostream& operator<< (std::ostream &out, cPoint &p) { out << p.get_x() << " " << p.get_y() << " " << p.get_z() << std

Purpose of returning by const value?

This question already has an answer here: What are the use cases for having a function return by const value for non-builtin type? 4 answers In the hypothetical situation where you could perform a potentially expensive non-const operation on an object, returning by const-value prevents you from accidentally calling this operation on a temporary. Imagine that + returned a non-const value, an

通过const值返回的目的?

这个问题在这里已经有了答案: 通过const值为非内建类型返回函数的用例是什么? 4个答案 在假设情况下,您可以对对象执行潜在的非常昂贵的非常量操作,通过const-value返回可以防止您意外地在临时调用此操作。 想象一下, +返回一个非const值,你可以写: (a + b).expensive(); 然而,在C ++ 11时代,强烈建议将值作为非常量返回,以便充分利用右值引用,这只对非常量右值有意义。 总之,这种做法有一个基本原理,但实

Post increment in operator overloading in c++

This is my post increment operator overloading declaration. loc loc::operator++(int x) { loc tmp=*this; longitude++; latitude++; retrun tmp; } My class constructor loc(int lg, int lt) { longitude = lg; latitude = lt; } In main function, I have coded like below int main() { loc ob1(10,5); ob1++; } While compiling this , i am getting the below error opover.cp

在c ++中增加运算符重载

这是我的后增量运算符重载声明。 loc loc::operator++(int x) { loc tmp=*this; longitude++; latitude++; retrun tmp; } 我的类构造函数 loc(int lg, int lt) { longitude = lg; latitude = lt; } 在主要功能中,我编码如下 int main() { loc ob1(10,5); ob1++; } 编译这个时,我得到下面的错误 opover.cpp:56:5:error:'loc loc :: operator ++(int)'的原型不匹配类'

Why can't a Visual C++ interface contain operators?

As per the MSDN doc on __interface, a Visual C++ interface "Cannot contain constructors, destructors, or operators." Why can't an interface contain an operator? Is there that much of a difference between a get method that returns a reference: SomeType& Get(WORD wIndex); and the overloaded indexer operator? SomeType& operator[](WORD wIndex); The __interface modifier is

为什么Visual C ++接口不能包含运算符?

根据__interface上的MSDN文档,Visual C ++接口“不能包含构造函数,析构函数或运算符”。 为什么接口不能包含运算符? 在返回引用的get方法之间是否有很大区别: SomeType& Get(WORD wIndex); 和重载的索引运算符? SomeType& operator[](WORD wIndex); __interface修饰符是用于帮助实现COM接口的Visual C ++扩展。 这允许你指定一个COM接口并强制执行COM接口规则。 由于COM是C兼容的定义,因此不能有运算符Cto

overloading the << operator in c++

hey, i got something that i cannot understand ,there are two types of solutions for overloading this operator 1 is including the friend at the start of the method and the other 1 goes without the friend. i would very much like if some1 explain whats the difference between them advantages / disadvantages. for example overloading the operator << in class rational: class Rational: { pri

在c ++中重载<<运算符

嘿,我得到了一些我无法理解的东西,有两种类型的解决方案用于重载这个算子1包括朋友在方法开始的时候,另外一个没有朋友。 如果有人解释他们之间的优点/缺点有什么区别,我会非常喜欢。 例如重载运算符<< in class rational: class Rational: { private: int m_t,m_b; ... friend ostream& operator<<(ostream& out,const Rational& r) // option 1 { return out << r.m_t &l