考慮以下內容,其中有些內容是通過多層添加到載體:傳遞參數爲const&或&&
class A {
public:
void Add(Content c) {
// Considerable amount of checking code here.
v.push_back(c);
}
private:
std::vector<Content> v;
};
class B {
public:
void Add(Content c) {
// Considerable amount of additional code here.
a.Add(c);
}
private:
A a;
};
class C {
public:
void Add(Content c) {
// Considerable amount of additional code here.
b.Add(c);
}
private:
B b;
};
這可以繼續,但你在這裏看到這一點。我希望通過複製或移動添加內容,即通過push_back(const Content &)或push_back(Content & &)添加內容。調用者應該能夠透過電話:
C c;
Content z;
c.Add(z);
或
c.Add(move(z));
,並得到副本數量最少。
有沒有一種方法來實現這個沒有重複的額外的代碼,並沒有使添加功能模板功能?
這不需要代碼重複(請參閱問題)? –