【发布时间】:2013-03-16 12:41:02
【问题描述】:
假设我有以下两个类:
class Person
{
public:
Person(string name, string surname)
: _name(move(name)), _surname(move(surname)) { }
...
private:
string _name;
string _surname;
};
class Student : public Person
{
public:
Student(string name, string surname, Schedule schedule)
: Person(move(name), move(surname)), _schedule(move(schedule)) { }
...
private:
Schedule _schedule;
};
int main()
{
Student s("Test", "Subject", Schedule(...));
...
return 0;
}
这是对移动语义的一种很好的用法吗?如您所见,Student 构造函数中有一层“move-s”。如果不使用const 引用将参数转发给基构造函数,是否可以避免move 函数调用开销?
或者也许..当我需要将参数转发给基本构造函数时,我应该使用 const 引用吗?
【问题讨论】:
标签: c++ c++11 move-semantics