标签: const-reference

为什么通过引用调用会创建一个新实例?

foo通过const ref以下方式调用方法:

// method, which is being called
void foo(const Entity & ent);

// call
Entity* e = new Entity;
foo(e);    // wrong: missing * but compiles
Run Code Online (Sandbox Code Playgroud)

这段代码不仅编译,而且还创建了一个新的实例,Entity其默认值在范围内foo.我希望这不会编译或至少崩溃.

如果我把foo正确的(foo(*e)),一切工作为疑似,我看到的正确的价值观Entityfoo.

我使用Qt 4.7提供的mingw.

这是以下界面Entity:

class Entity : public QObject
{
    Q_OBJECT

public:
    Entity (QObject* parent = NULL);

    long getId() const { return this->id; }
    void setId(const long id) { this->id = id; }

    QString …
Run Code Online (Sandbox Code Playgroud)

c++ qt const-reference

8
推荐指数
1
解决办法
311
查看次数

警告C4172:返回对绑定到局部变量的const std :: string的引用.它有多安全?

我刚刚在工作中构建了一个项目,我看到添加了一个新功能:

const std::string& ClassName::MethodName() const
{
   return "";
}
Run Code Online (Sandbox Code Playgroud)

编译器发出警告:

警告C4172:返回本地变量的地址或临时变量

我认为编译器是对的.这个功能有多安全?

请注意,const char*由于字符串文字具有静态存储持续时间,因此函数不会返回哪个是正常的.它返回一个引用const std::string

c++ compiler-warnings object-lifetime const-reference

7
推荐指数
1
解决办法
5262
查看次数

将const引用返回给本地对象时到底发生了什么?

struct A {
    A(int) : i(new int(783)) {
        std::cout << "a ctor" << std::endl;
    }

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

    ~A() {
        std::cout << "a dtor" << std::endl;
        delete i;
    }

    void get() {
        std::cout << *i << std::endl;
    }

private:
    int* i;
};

const A& foo() {
    return A(32);
}

const A& foo_2() {
    return 6;
}

int main()
{
    A a = foo();
    a.get();
}
Run Code Online (Sandbox Code Playgroud)

我知道,返回对本地值的引用是不好的.但是,另一方面,const引用应该延长临时对象的生命周期.

此代码生成UB输出.所以没有生命延伸.

为什么?我的意思是有人可以解释一下子发生什么事吗? …

c++ const-reference

7
推荐指数
2
解决办法
3654
查看次数

在进行类型转换时,Visual Studio不会创建临时对象?

我正在使用Visual Studio Express 2013,并且在尝试学习C++中的不同内容时有点愚弄.

我在编译器中偶然发现了一个有趣的错误,当显式地将类型转换为与引用相同的类型时,它似乎不会创建临时对象.

#include <iostream>

using namespace std;

int main()
{
    int number; // float number;
    number = 2;

    const int& plainref_i = number;
    const int& recastref_i = (int)number; // this goes wrong if number is int
    const float& plainref_f = number;
    const float& recastref_f = (float)number; // this goes wrong if number is float

    number = 3;

    std::cout << plainref_i << "\n";
    std::cout << recastref_i << "\n";
    std::cout << plainref_f << "\n";
    std::cout << recastref_f << "\n"; …
Run Code Online (Sandbox Code Playgroud)

c++ casting const-reference temporary-objects visual-studio-2013

7
推荐指数
1
解决办法
303
查看次数

带有 ref 限定符的 getter 的最佳实践

以下代码会导致未定义的行为:

class T
{
public:
    const std::string& get() const { return s_; }

private:
    std::string s_ { "test" };
}

void breaking()
{
    const auto& str = T{}.get();
    // do sth with "str" <-- UB
}
Run Code Online (Sandbox Code Playgroud)

(因为const&根据我的理解,生命周期延长不适用于此处)。

为了防止这种情况,一种解决方案可能是添加一个引用限定符get()以防止在 LValues 上调用它:

const std::string& get() const & { return s_; }
Run Code Online (Sandbox Code Playgroud)

但是,由于函数现在既是const又是&合格的,因此仍然可以调用get()RValues,因为它们可以分配给const&

const auto& t = T{};        // OK
const auto& s1 = t.get();   // OK
const auto& s2 = …
Run Code Online (Sandbox Code Playgroud)

c++ const-reference ref-qualifier

7
推荐指数
1
解决办法
93
查看次数

将rvalue引用传递给const lvalue reference paremeter

我试图理解C++ 11 rvalue引用以及如何在我的代码中使用它们以获得最佳性能.

假设我们有一个类A,它有一个指向大量动态分配数据的成员指针.

此外,一种foo(const A& a)与类对象做某事的方法A.

我希望防止在将A对象A传递给函数时调用复制构造函数foo,因为在这种情况下它将执行底层堆数据的深层复制.

我测试了传递左值参考:

    A a;
    foo(a);
Run Code Online (Sandbox Code Playgroud)

并传递右值引用:

    foo(A());
Run Code Online (Sandbox Code Playgroud)

在这两种情况下都没有调用复制构造函数.

这是预期的还是由于我的编译器(Apple LLVM 5.1)的一些优化?这有什么说明吗?

c++ rvalue lvalue const-reference c++11

6
推荐指数
1
解决办法
6115
查看次数

返回子类的const引用

我知道的

我知道返回一个临时对象的const引用是可以的!(像这个例子:)

class A {
public:
  virtual const A& clone () { return (A()); }
  virtual std::string name() const { return ("A"); }
};
Run Code Online (Sandbox Code Playgroud)

返回临时对象并绑定到const引用

但!

如果我想这样做,它仍然是正确的:

class B : public A {
public:
  virtual const A& clone () { return (B()); }
  virtual std::string name() const { return ("B"); }
};
Run Code Online (Sandbox Code Playgroud)

我认为是的,但在执行时,返回的对象仍被视为A对象(如本例:)

main.cpp中

#include <iostream>
#include <string>
int main() {
  B bb;
  A* aa = &bb;

  std::cout << aa->clone().name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

产量

valgrind ./a.out
==14106== Use of uninitialised …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance const-reference

6
推荐指数
1
解决办法
550
查看次数

是否在?:expression中创建的C++临时对象的生命周期是通过将其绑定到本地const引用来扩展的?

我不清楚临时对象的生命周期是通过将它绑定到?:表达式中的const引用来扩展的:

class Foo {...};

Foo *someLValue = ...;

const Foo& = someLValue ? *someLValue : Foo();
Run Code Online (Sandbox Code Playgroud)

通过将默认构造函数Foo()通过绑定到本地const ref来扩展创建临时文件的生命周期,即使绑定是有条件的吗?或者这会创建一个悬空引用,因为Foo()的临时值将在?:表达式的末尾被销毁?

c++ reference const-reference temporary-objects

6
推荐指数
1
解决办法
231
查看次数

对函数作用域(生命周期)后的临时对象的const引用被破坏

在提出这个问题的时候,我学会了对一个临时对象的const引用在C++中是有效的:

int main ()
{
  int a = 21;
  int b = 21;

  //error: invalid initialization of non-const reference
  //int     & sum = a + b;e [...]

  //OK
  int const & sum = a + b;

  return sum;
}
Run Code Online (Sandbox Code Playgroud)

但在下面的示例中,const引用refnop引用了一个被销毁的临时对象.我想知道为什么?

#include <string>
#include <map>

struct A
{
   // data 
   std::map <std::string, std::string>  m;
   // functions
   const A& nothing()           const { return *this;    }
   void init()                        { m["aa"] = "bb";  }
   bool operator!= (A const& a) const …
Run Code Online (Sandbox Code Playgroud)

c++ scope reference const-reference temporary-objects

5
推荐指数
1
解决办法
1547
查看次数

如果C++类包含const引用和非const引用复制构造函数怎么办?

摘录1:

#include<iostream>
using namespace std;

class C{
public:
    C(){}
    C(const C& c){
        cout<<"const copy constructor called"<<endl;
    }
};
int main(){
    C c1;
    C c2 = c1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

output:const复制构造函数调用


摘录2:

#include<iostream>
using namespace std;

class C{
public:
    C(){}
    C(const C& c){
        cout<<"const copy constructor called"<<endl;
    }
    C(C& c){
        cout<<"non-const copy constructor called.\t "<<endl;
    }
};
int main(){
    C c1;
    C c2 = c1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:调用非const复制构造函数


摘录3:

#include<iostream>
using namespace std;

class C{
public:
    C(){}
    C(const …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor const-reference

5
推荐指数
1
解决办法
334
查看次数