我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)),一切工作为疑似,我看到的正确的价值观Entity中foo.
我使用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) 我刚刚在工作中构建了一个项目,我看到添加了一个新功能:
const std::string& ClassName::MethodName() const
{
return "";
}
Run Code Online (Sandbox Code Playgroud)
编译器发出警告:
警告C4172:返回本地变量的地址或临时变量
我认为编译器是对的.这个功能有多安全?
请注意,const char*由于字符串文字具有静态存储持续时间,因此函数不会返回哪个是正常的.它返回一个引用const std::string
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输出.所以没有生命延伸.
为什么?我的意思是有人可以解释一下子发生什么事吗? …
我正在使用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
以下代码会导致未定义的行为:
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++ 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)的一些优化?这有什么说明吗?
我知道返回一个临时对象的const引用是可以的!(像这个例子:)
class A {
public:
virtual const A& clone () { return (A()); }
virtual std::string name() const { return ("A"); }
};
Run Code Online (Sandbox Code Playgroud)
如果我想这样做,它仍然是正确的:
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) 我不清楚临时对象的生命周期是通过将它绑定到?:表达式中的const引用来扩展的:
class Foo {...};
Foo *someLValue = ...;
const Foo& = someLValue ? *someLValue : Foo();
Run Code Online (Sandbox Code Playgroud)
通过将默认构造函数Foo()通过绑定到本地const ref来扩展创建临时文件的生命周期,即使绑定是有条件的吗?或者这会创建一个悬空引用,因为Foo()的临时值将在?:表达式的末尾被销毁?
在提出这个问题的时候,我学会了对一个临时对象的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) 摘录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++ ×10
const-reference ×10
reference ×2
c++11 ×1
casting ×1
inheritance ×1
lvalue ×1
qt ×1
rvalue ×1
scope ×1