小编Gil*_*Gil的帖子

c ++ - 构造和破坏的顺序

我有以下C++代码(VS2013):

#include <iostream>
using namespace std;
class A {
    int i;
public:
    A(int i) : i(i) {
        cout << "DEFAULT CTOR " << i << endl;
    }
    A(const A &o) : i(o.i) {
        cout << "COPY CTOR " << i << endl;
    }
    ~A() {
        cout << "DTOR " << i << endl;
    }
    friend A f(const A &, A, A *);
};
A f(const A &a, A b, A *c) {
    return *c;
}
int main() {
    f(1, A(2), …
Run Code Online (Sandbox Code Playgroud)

c++ constructor destructor reference

3
推荐指数
1
解决办法
428
查看次数

将右值绑定到左值引用

我有以下C ++代码(VS2013):

#include <iostream>
using namespace std;

class A {
    int i;
public:
    A(int i) : i(i) {
        cout << "Constructor: " << i << endl;
    }
    A(const A &o) : i(o.i) {
        cout << "Copy constructor: " << i << endl;
    }
    ~A() {
        cout << "Destructor: " << i << endl;
    }
};

A test(const A &a, A b, A *c) {
    return *c;
}

int main() {
    A b(10);
    cout << "START OF TEST" << endl;
    test(1, …
Run Code Online (Sandbox Code Playgroud)

c++ constructor type-conversion copy-constructor

0
推荐指数
1
解决办法
1318
查看次数