假设T是一个C++类,如果我这样做T a = b;,是复制构造函数还是赋值运算符?
我当前的实验显示复制构造函数被调用,但不明白为什么.
#include <iostream>
using namespace std;
class T {
public:
// Default constructor.
T() : x("Default constructor") { }
// Copy constructor.
T(const T&) : x("Copy constructor") { }
// Assignment operator.
T& operator=(const T&) { x = "Assignment operator"; }
string x;
};
int main() {
T a;
T b = a;
cout << "T b = a; " << b.x << "\n";
b = a;
cout << "b = a; …Run Code Online (Sandbox Code Playgroud) 在 c++98 中,以下程序应调用复制构造函数。
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "default" ; }
A(int i) { cout << "int" ; }
A(const A& a) { cout << "copy"; }
};
int main ()
{
A a1;
A a2(0);
A a3 = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果您在上述情况下显式声明复制构造函数(编译器出错),这很明显。但是当它未声明为显式时,我看不到复制构造函数的输出。我想这是因为复制省略。有什么方法可以禁用复制省略或标准是否强制要求?
类的移动构造函数接受可以引用临时对象的rvalue引用.所以,我有临时对象和适当的移动构造函数,它可以接受对临时对象的引用,但是移动构造函数不会被调用.怎么了?
//g++ 5.4.0
#include <iostream>
class foo
{
int data;
public:
foo(int v) : data(v) {std::cout << "foo(int)\n";}
foo(foo&& f)
{
std::cout << "moved\n";
}
void print()
{
std::cout << data;
}
};
void acceptTmp(foo f)
{
f.print();
}
int main()
{
foo f1 = foo(100);
f1.print();
acceptTmp(foo(200)); //also does not move
}
Run Code Online (Sandbox Code Playgroud) 今天,我遇到了一些我对复制构造函数不太了解的内容。
考虑下面的代码:
#include <iostream>
using namespace std;
class some_class {
public:
some_class() {
}
some_class(const some_class&) {
cout << "copy!" << endl;
}
some_class call() {
cout << "is called" << endl;
return *this; // <-- should call the copy constructor
}
};
some_class create() {
return some_class();
}
static some_class origin;
static some_class copy = origin; // <-- should call the copy constructor
int main(void)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后在将原点分配给副本时调用副本构造函数,这很有意义。但是,如果我将复制声明更改为
static some_class copy = some_class();
Run Code Online (Sandbox Code Playgroud)
它没有被调用。即使在使用该create()函数时,它也不会调用复制构造函数。但是,将其更改为
static …Run Code Online (Sandbox Code Playgroud) 可能重复:
什么是复制省略和返回值优化?
我有以下程序:
#include <iostream>
using namespace std;
class Pointt {
public:
int x;
int y;
Pointt() {
x = 0;
y = 0;
cout << "def constructor called" << endl;
}
Pointt(int x, int y) {
this->x = x;
this->y = y;
cout << "constructor called" << endl;
}
Pointt(const Pointt& p) {
this->x = p.x;
this->y = p.y;
cout << "copy const called" << endl;
}
Pointt& operator=(const Pointt& p) {
this->x = p.x;
this->y = p.y;
cout …Run Code Online (Sandbox Code Playgroud) 为什么g_Fun()执行return temp它会调用复制构造函数?
class CExample
{
private:
int a;
public:
CExample(int b)
{
a = b;
}
CExample(const CExample& C)
{
a = C.a;
cout<<"copy"<<endl;
}
void Show ()
{
cout<<a<<endl;
}
};
CExample g_Fun()
{
CExample temp(0);
return temp;
}
int main()
{
g_Fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud) Line Line::operator =(Line ln) {
cout << "Assignment operator\n";
Line temp;
temp.ptr = new int;
*temp.ptr = *(ln.ptr);
return temp;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,执行以下语句时不会调用复制构造函数:
return temp;
Run Code Online (Sandbox Code Playgroud)
由于返回是按值,为什么不调用复制构造函数?
谢谢
例如,我有一个类在其consturctor中调用一个返回本地对象的函数.我正在尝试使用rvalue引用来访问此对象,以避免在内存中进行昂贵的移动.
class MyClass
{
BigObject&& C;
MyClass() : C(f())
{
};
};
BigObject f()
{
return BigObject();
}
Run Code Online (Sandbox Code Playgroud)
但是compiller告诉我,引用成员被初始化为一个临时的,在构造退出后不会持续存在.
我不明白.我理解在函数范围内创建的本地对象仅存在于函数范围内.到达范围的末尾 - 调用本地对象的析构函数.在这里,我用本地对象初始化rvalue引用,并且我可以访问它,而我在constuctor的主体中.
有人可以解释一下,这里发生了什么?有没有办法返回一个本地对象并将其用作任何ligetable类成员,而不是在内存中移动它?
#include <iostream>
using namespace std;
class myclass {
public:
myclass();
myclass(const myclass &o);
myclass f();
};
myclass:: myclass(){
cout<<"Constructing normally"<<endl;
};
myclass:: myclass(const myclass &o){
cout<<"Constructing copy"<<endl;
};
myclass myclass::f(){
myclass temp;
return temp;
};
int main(){
myclass obj;
obj = obj.f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在一本书中找到了这个例子,该书显示该程序的输出应该是:
Constructing normally
Constructing normally
Constructing copy
Run Code Online (Sandbox Code Playgroud)
但是当我在我的编译器中编译它时,它只显示下面写的输出
Constructing normally
Constructing normally
Run Code Online (Sandbox Code Playgroud)
里面究竟发生了什么?