所有,我正在玩弄了解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)'
答案 0 :(得分:1)
我认为你已经成为Scott Meyers所说的C ++最烦恼的解决方案的受害者。 https://en.wikipedia.org/wiki/Most_vexing_parse