Say we have an abstract base class IBase
with pure virtual methods (an interface).
Then we derive CFoo
, CFoo2
from the base class.
And we have a function that knows how to work with IBase.
Foo(IBase *input);
The usual scenario in these cases is like this:
IBase *ptr = static_cast<IBase*>(new CFoo("abc"));
Foo(ptr);
delete ptr;
But pointer management is better to be avoided, so is there a way to use references in such scenario?
CFoo inst("abc");
Foo(inst);
where Foo
is:
Foo(IBase &input);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…