class B
{
public:
A* GetA() const { return obj; }
private:
A* obj;
}
...
B b;
b.GetA()->AInterfaceMethod(params);
So my questions are:
- What would be different had the function not been const?
- Are there any restrictions to what I can do with the pointer obtained via GetA()? Is it const? Does it point to a const A?
- When is this useful?
I encountered this in an Unreal tutorial (A
is forward declared in the tutorial).
EDIT: I probably messed up, but the call does work. I've included the actual Unreal code below:
class ABatteryPickup : public APickup
{
ABatteryPickup()
{
GetMesh()->SetSimulatePhysics(true);
}
}
class Pickup
{
public:
class UStaticMeshComponent* GetMesh() const { return PickupMesh; }
private:
class UStaticMeshComponent* PickupMesh;
}
UStaticMeshComponent::SetSimulatePhysics()
is not const
.
Also, just tested this on a new, clean C++ project, and it works.