Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force a class to be inherited?

Consider the following class:

//! Counter class base. Must be inherited.
class base_seeded_counter
{
public:
    //! Destructor
    virtual ~base_seeded_counter() { };

    //! Get the next value.
    int get_next();

protected:
    //! Override the counter next value.
    void set_next(const int next);

private:
    int m_next_value;    // Next value.
}

// ------------------------------------------------------

inline int base_seeded_counter::get_next()
{
    return m_next_value++;
}

// ------------------------------------------------------

inline void base_seeded_counter::set_next(const int next)
{
    m_next_value = next;
}

The purpose of this class is to implement a base counter object with the intention that it must be inherited. It does not have any virtual methods other than the destructor and, more importantly, does not initialize the m_next_value member. This is the job of the derived class. For example:

class file_seeded_counter : public base_seeded_counter
{
public:
    file_seeded_counter(const std::string &file_name);

    void reseed();

private:
    std::string m_file_name;
}

inline int file_seeded_counter::file_seeded_counter(const std::string &file_name) :
    m_file_name(file_name)
{
    reseed();
}

inline void file_seeded_counter::reseed()
{
    int seed_from_file;

    // open file here and get seed value...

    set_next(seed_from_file);
}

This class, deriving from base_seeded_counter reads the initial counter value from a file, and offers the ability to re-read the seed from the file via the reseed() method. There may be other classes that offer similar functionality to seed from databases, network sources, or a PRNG, for example.

My question is this: Given that I have no pure virtual methods, does C++ offer a mechanism to prevent someone from creating an instance of base_seeded_counter?

like image 215
Karl Nicoll Avatar asked Dec 03 '22 14:12

Karl Nicoll


1 Answers

My question is this: Given that I have no pure virtual methods, does C++ offer a mechanism to prevent someone from creating an instance of base_seeded_counter?

Yes, give it a protected default constructor (may be empty).

like image 179
πάντα ῥεῖ Avatar answered Dec 23 '22 05:12

πάντα ῥεῖ