How do you assert that an object has a certain method in c++? Or throw an exception when you try to call it and it isn't there? Is this what I should be doing to handle my problem?
I have a container class with a custom object inside of it. In a container method, I want to call the 'smaller' object's method, but I need to make sure it exists first.
I probably shouldn't make the desired method a pure virtual function in this class because it is also used in other ways by different container classes that don't need it to be this way. Right?
C++ does not provide a runtime-check if a method exists for a particular object at hand. But you can overcome this in two principal ways: (1) use (pure) virtual functions at some common base class level, or (2) check the type of the object at hand.
(1) pure virtual function approach:
class ContainerObject {
...
virtual void somethingSpecific() = 0;
}
class MyContainerObject : public ContainerObject {
...
virtual void somethingSpecific() { ... }
}
Thereby, if you get a pointer to an object of type ContainerObject
, you can rely on the existence of member function somethingSpecific()
; Note that class ContainerObject
is abstract, as it contains a pure virtual function, i.e. a virtual member function without implementation:
ContainerObject *o = someContainer.getSomeObject();
o->somethingSpecific(); // compiler checks existence of `somethingSpecific`.
(2) a type check approach:
However, if you do not want to expose somethingSpecific
at a general level, you could use type checks, e.g. with dynamic casts. Suppose a similar example as above, yet without the pure virtual function somethingSpecific
at the level of ContainerObject
class:
class ContainerObject {
...
virtual void anyOtherVirtualFunction();
}
class MyContainerObject : public ContainerObject {
...
virtual void somethingSpecific() { ... }
}
The runtime type check based on dynamic cast then tries to interpret the object returned by getSomeObject
as a MyContainerObject
:
MyContainerObject *o = dynamic_cast<MyContainerObject*>(someContainer.getSomeObject());
if (o != nullptr) // does o point to a MyContainerObject?
o->somethingSpecific();
Note that getSomeObject
might return an object other than a MyContainerObject
. In this case, the result of the dynamic cast would be null. Hence, if the result is not null, then you can rely that o
points to a MyContainerObject
-instance (which implements somethingSpecific
).
Note further, that dynamic cast requires that polymorphism is in place, which means that the base class ContainerObject
must have at least one virtual member function (anyOtherVirtualFunction
in this example).
Hope this helps a bit.