I have a few questions regarding differentiation between const and non-const versions of methods in C++.
Example:
MyObject* MyClass::objectReference()
const MyObject* MyClass::objectReference() const
My questions are:
- Is there any way at all to differentiate between which version of the method is called manually? Or is it fully/completely automatic and if so what are the precise rules for determining which version is to be called?
- Related to (1), if you cannot differentiate between calling the const vs the non-const version, it’s therefore impossible to call one version of a method from the other to prevent duplication?
How to create a link to one version or the other in the documentation of one of the methods using doxygen? (e.g. “const version of myMethod().” or “non-const version of myMethod().”)Found this out myself – just add or omit ” const” at the end of the method signature.
I don’t know about doxygen; though here is what I know.
constversion, it simply can’t be called on aconstobject.constversion, it can be called on bothconstand non-constobjects.constversion will be called on non-constobjects, and theconstversion will be called onconstobjects.constone, you have to cast your object to a const reference of itself:static_cast<const MyClass&>(myObject).objectReference();