9得票7回答
在失败的情况下如何返回一个const QString引用?

考虑以下代码: const QString& MyClass::getID(int index) const { if (i < myArraySize && myArray[i]) { return myArray[i]->id;...

9得票2回答
返回本地对象的const引用时会发生什么?

struct A { A(int) : i(new int(783)) { std::cout << "a ctor" << std::endl; } A(const A& other) : i(new int(*(ot...

9得票2回答
带const引用的可变参数模板特化

如何为具有const引用参数的可变模板函数提供专业知识? 例子: template<typename T, typename... Args> T foo(Args... args) = delete; template<> int foo(int a, cons...

8得票3回答
为什么这个引用调用会创建一个新实例?

我正在通过const ref调用一个名为foo的方法: // method, which is being called void foo(const Entity & ent); // call Entity* e = new Entity; foo(e); // wron...

8得票2回答
防止临时变量延长其生命周期?

这可能是不可能的,但我想知道是否可以让临时变量永远保持在其原始表达式之后。我有一系列指向父对象的对象链,以及一个创建子对象的成员函数,以下是一个简化的示例: class person{ string name; person * mommy; public: pers...

8得票4回答
警告 C4172:返回绑定到本地变量的 const std::string 的引用。这样做安全吗?

我刚在工作中构建我们的一个项目,发现增加了一个新的功能: const std::string& ClassName::MethodName() const { return ""; } 编译器给出了警告: 警告 C4172:返回局部变量或临时变量的地址 我认为编译器...

7得票1回答
Visual Studio在类型转换时未创建临时对象?

我正在使用Visual Studio Express 2013并尝试学习C++的不同知识点。我发现编译器中存在一个有趣的bug,即当显示地将类型转换为引用相同的类型时,似乎不会创建临时对象。 #include <iostream> using namespace std; i...

7得票1回答
带有引用限定符的getter函数的最佳实践

以下代码会导致未定义的行为: class T { public: const std::string& get() const { return s_; } private: std::string s_ { "test" }; } void breaking()...