Deutsch
Germany.ruФорумы → Архив Досок→ Программирование

C++11 лажа?

20.08.16 12:53
Re: C++11 лажа?
 
AlexNek патриот
AlexNek
в ответ anly 19.08.16 10:43

Я хоть уже всё и забыл, но что то не врублюсь в проблему. Всё работает как и должно при чём тут оптимизация?

#include "stdafx.h"
#include <iostream>
using namespace std;

class DDD
{
    void operator =(const DDD&);
    void operator =(DDD&&);
public:
    int xxx;
    DDD() : xxx(0){ cout << "ctor1" << endl; }
    DDD(const DDD& a) : xxx(a.xxx + 1){ cout << "ctor2" << endl; }
    DDD(DDD&& a) : xxx(a.xxx + 2){ cout << "ctor3" << endl; }
};

DDD ddd()
{
    cout << "f1" << endl;
    return DDD();
}

int _tmain(int argc, _TCHAR* argv[])
{
    DDD d = ddd();
    cout << d.xxx << endl; // print 0

    DDD d1 = DDD(d);
    cout << d1.xxx << endl; // print 1

    DDD d2 = DDD(DDD()); // an rvalue reference
    cout << d2.xxx << endl; // print 2
    return 0;
}
/* ---output---
f1
ctor1
0
ctor2
1
ctor1
ctor3
2
*/
 

Перейти на