mardi 29 novembre 2016

In c++ how do I successfully chain classes together while sharing variables in the following example?

In c++ how do I successfully chain classes together while sharing variables in the following example?

Clock timer;

std::cout << timer.getElapsedTime().inSeconds();
std::cout << timer.getElapsedTime().inMilliseconds();

If I try to compile I get 'start' not declared in this scope. If I try to inherit class Clock with: ' class ElapsedTime: public Clock ' I get Error expected class name before '{' token.

How do I write the code successfully?

class ElapsedTime
{
    double inSeconds() const
    {
        return (std::clock() - start) / (double) CLOCKS_PER_SEC);
    }

    double inMilliseconds() const
    {
        return ((std::clock() - start) / (double) CLOCKS_PER_SEC) * 1000;
    }
};


class Clock
{
public:

    Clock()
    {
        start = std::clock();
    }

    ElapsedTime const & getElapsedTime() const {
        return ???object_here;
    }

    double reset()
    {
        duration = (std::clock() - start ) / (double) CLOCKS_PER_SEC;
        start = std::clock();
        return duration;
    }

protected:
    std::clock_t start;

private:
    double duration;

};

Aucun commentaire:

Enregistrer un commentaire