samedi 6 octobre 2018

How to store / call all methods via a class pointer

I have a particular requirement for a module implementation. There are multiple clients means that there are different classes each having its own set of public methods. There is a framework that will send the clients object pointers to the main worker class. Now it is easy to store the pointers in array but then how the server know what are the methods to call:

In below example can the Counter class by any mechanism access all the methods its derived class:

#include <cstddef>
#include <iostream>
template<typename T>
class Counter {
public:
    Counter() { mycount = ++count ;}
    Counter(const Counter&)  { mycount = ++count; }
    virtual ~Counter() { mycount = --count; }

    static size_t howMany() {  return count; }
    size_t mycount() {  return mycount; }
private:
    size_t mycount;
    static size_t count;
};
template<typename T>
size_t Counter<T>::count = 0;


class Widget: private Counter<Widget> {
public:
    using Counter<Widget>::mycount; 
    size_t GetCount (){ return Counter<Widget>::mycount();}
    // rest methods of Widget is unchanged
    void Show();
};

class ABCD: private Counter<ABCD> {
public:
    // make howMany public
    using Counter<ABCD>::mycount;
    size_t GetCount (){ return Counter<ABCD>::mycount();}
    // rest of ABCD is unchanged
    void NoShow();
};

int main()
{
Widget A, B;
ABCD g;
}

Aucun commentaire:

Enregistrer un commentaire