puppy puppy - 3 days ago 6
C++ Question

What do you call a member function that follows a member function and how do I write one?

What do you call function that follows a member function and modifies the return value and how do I write one?

In other words how do I successfully write:

std::cout << box.getVolume().inCubicFeet();

std::cout << box.getVolume().inCubicCentimeters();

M.M M.M
Answer

For that to work getVolume() needs to return an object of type Volume ( or even a reference to an object of type Volume &), so that whatever method follows you are able to invoke it on said object. For instance:

class Volume{
    ...
    int inCubicFeet() const {
        //convert it and return it
    }
    int inCubicCentimeters() const {
        //convert it and return it
    }
};


class Box{
    ...
    Volume const& getVolume() const {
        return volumeObject;
    }
};

Note: to return a reference you could do:

Volume const & getVolume() const {
    return volumeObject;
}
Comments