0
所有的,我玩弄瞭解r值,l值和scott meyers稱之爲「通用引用」。我在設置示例時遇到了一些問題,如下所示。誰能提供一些指導?移動構造函數抱怨「非類類型」
#include <cstdio>
namespace who_needs_namespaces {
class SomeSillyClass {
public:
SomeSillyClass() {
printf("Default constructed.\n");
}
SomeSillyClass(const SomeSillyClass& rhs) {
printf("L-Value constructed.\n");
}
SomeSillyClass(SomeSillyClass&& rhs) {
printf("R-Value Constructed.\n");
}
void doStuff() const {
printf("doing...\n");
}
};
template<typename T>
class HolderByValue {
public:
HolderByValue() = delete;
HolderByValue(T toHold) : m_mine(toHold)
{ };
private:
T m_mine;
};
template<typename T>
class HolderByRef {
public:
HolderByRef() = delete;
HolderByRef(T&& toHold) : m_mine(toHold)
{ };
private:
T m_mine;
};
}
using namespace who_needs_namespaces;
int main() {
//Check our silly class.
SomeSillyClass defaultCtr;
defaultCtr.doStuff();
SomeSillyClass copyCtr(defaultCtr);
copyCtr.doStuff();
SomeSillyClass moveCtr(SomeSillyClass);
moveCtr.doStuff();
//Check our HolderByValue
//HolderByValue<SomeSillyClass> holder(defaultCtr);
HolderByValue<SomeSillyClass &> holder(defaultCtr);
}
main.cpp: In function 'int main()':
main.cpp:59:13: error: request for member 'doStuff' in 'moveCtr', which is of non-class type 'who_needs_namespaces::SomeSillyClass(who_needs_namespaces::SomeSillyClass)'
你想用'SomeSillyClass moveCtr(SomeSillyClass)來做什麼;'?將類型傳遞給構造函數?聲明一個函數...我懷疑編譯器認爲後者 – doctorlove
嘗試'SomeSillyClass moveCtr((SomeSillyClass()));' – doctorlove