Ste*_*tef 0 c++ pointers reference class
在我的C++类中,我需要分配一个带指针和/或引用的方法.所以我做了这个棘手的事情:
(假设aclass是一个类变量AnotherClass*)
void MyClass::setElem(AnotherClass *pVal)
{
aclass = pVal;
}
void MyClass::setElem(AnotherClass &refVal)
{
aClass = &article;
}
Run Code Online (Sandbox Code Playgroud)
但在我看来,听起来并不那么"优雅"......
更好的方法来实现这一目标
void MyClass::setElem(AnotherClass *pVal)
{
aclass = pVal;
}
void MyClass::setElem(AnotherClass &refVal)
{
setElem(&refVal);
}
Run Code Online (Sandbox Code Playgroud)
这个优雅吗?正如皮特先生曾经在赛因菲尔德所说的那样"好吧,你不需要太多恩典,否则你将无法忍受".