Statically Init a derived class - c++

With c++, Is there a way to get a derived class to inherit its own static initializer? I am trying to do something like the following:
class Base {
public:
class StaticInit {
public:
virtual StaticInit() =0;
};
};
class Derived: public Base {
public:
virtual StaticInit::StaticInit() {
//do something with the derived class
}
static StaticInit init;
}
static Derived::StaticInit init;
it would also be nice if I didn't have to define the init var in each derived class. I am currently redefining the StaticInit internal class in each derived class but it seems redundant.
Each derived class is a singleton, and I need the instance to be stored in a lookup table at program startup.

I use templates to do this, rather than inheritance - something like:
template <typename T>
struct StaticInit {
StaticInit() {
// do whatever with T
}
};
in use:
static StaticInit <SomeClass> init;

Use the CRTP:
template <class D>
class Base {
public:
class StaticInit {
public:
StaticInit() { D::initStatic(*this); }
};
static StaticInit init;
};
template <class D>
Base<D>::StaticInit init;
class Derived: public Base<Derived> {
public:
initStatic(Base<Derived>::StaticInit& s) {
// Do derived-specific initialisation on s.
}
}

Related

C++ SafeSingleton 3 level inheritance

I have a templated SafeSingleton class, Base class which is derived from SafeSingleton and implements some base methods. I want to have class that is derived from Base and can be accessed via instance() method of SafeSingleton. The problem is that when I am trying to access Derived::instance() it returns the pointer to a Base class and the compiler doesn't know anything about methods of derived class. What should I do to make below code work.
template<class T>
class SingleTon {
public:
static T* instance()
{
return holder().instance;
}
protected:
template<class I>
struct Holder
{
Holder() : instance(new I())
{
}
I* instance;
};
static Holder<T> &holder()
{
static Holder<T> holder;
return holder;
}
};
// Hopefully issue is here, I am never creating SingleTon<Derived>, but how can it be done?
class Base : public SingleTon<Base> {
public:
Base() = default;
void printBase() {
std::cout << "Base";
}
};
class Derived : public Base {
public:
Derived() = default;
void printDerived() {
std::cout << "Derived";
}
};
int main()
{
Derived::instance()->printBase();
Derived::instance()->printDerived(); // Here is the error
//Error: main.cpp:57:26: error: ‘class Base’ has no member named ‘printDerived’
//57 | Derived::instance()->printDerived();
return 0;
}
template<class D>
class Base : public SingleTon<D> {
and
class Derived : public Base <Derived>
and ... done?
If you want to put Base's non-Ddependent methods in a cpp file, you'll have to get fancy. Have BaseImp that does not derive from SingleTon, put code there. Have Base<D> derive from it and write forwarding glue to it BaseImpl. But you probably don't need this.

C++ class Design with Inheritance

class Base
{
public:
static int GetType_S()
{
return 1;
}
virtual int GetType()
{
return GetType_S();
}
};
class Son1 : public Base
{
public:
static int GetType_S()
{
return 2;
}
virtual int GetType()
{
return GetType_S();
}
};
My Question is : When I need some other classes like "Son1,Son2..." ,every class should implement GetType and GetType_S function, This two functions is repeated.So How to design this classes gracefully to let son classes implement only one function?And It is best not to use macros.
You can have a class template that each SonN inherits from, which in turn inherits Base.
template <int Type>
class Middle : public Base
{
public:
static int GetType_S()
{
return Type;
}
virtual int GetType()
{
return GetType_S();
}
};
class Son1 : public Middle<2> {};
class Son2 : public Middle<3> {};
class Whatever : public Middle<42> {};
Or, if there is nothing else in those classes:
using Son1 = Middle<2>;
using Son2 = Middle<3>;
using Whatever = Middle<42>;

Static function in template class

I'm trying to create a sort of factory class in a template. I wanted to do something like a pure virtual function, but it needs to be static as I'm using the function to create types.
What I want to happen is when I declare a class, the template calls the static function. The static function is actually declared in the templatised class.
I've got as far as:
class Base
{
};
template<typename T>
class Type : public Base
{
public:
static void Create()
{
mBase = CreateBase();
}
private:
static Base* CreateBase();
static Base* mBase;
};
class MyType : public Type<MyType>
{
private:
static Base* CreateBase()
{
return new MyType;
}
};
template<typename T>
Base* Type<T>::mBase = NULL;
void test()
{
MyType::Create();
}
I get a link time error:
undefined reference to `Type<MyType>::CreateBase()
The CreateBase function is defined in the base type, so just call it:
template<typename T>
class Type : public Base
{
public:
static void Create()
{
mBase = Base::CreateBase();
}
//...
There is no need to declare another CreateBase in the template.
Found it.
The problem was I wasn't calling the derived class' function.
Here is the fix:
static void Create()
{
mBase = T::CreateBase();
}

Refer to derivative function

I have a base class and a few derivative. I have to 'register' some static function from each of them. Here is the example:
class Base
{
// Some interface...
};
class Der1 : Base
{
static void Do();
};
class Der2 : Base
{
static void Do();
};
void processStatic()
{
SomeFunc(Der1::Do);
SomeFunc(Der2::Do);
}
As you see, SomeFunc receives function pointer. I want to do that automatically with each new derivative class, is it possible? Maybe, predefine static function in Base class and register it there. But I think it's impossible, yes?
Maybe, this will be more easier to understand what do I want:
class Der1 : Base
{
Der1() { SomeFunc(Der1::Do); }
static void Do();
};
class Der2 : Base
{
Der2() { SomeFunc(Der2::Do); }
static void Do();
};
EDIT: Completely replacing previous answer due to clarified requirements.
You could use the CRTP to declare a specialized base class that does nothing more than call your registration function:
#include <iostream>
void SomeFunc(void(*fp)()) {
(*fp)();
};
template <class D>
struct ExtraBass {
ExtraBass() {
static bool once;
if(!once)
SomeFunc(D::Do);
once = true;
}
};
struct Bass {
};
struct Drive : Bass, ExtraBass<Drive> {
static void Do() { std::cout << "Drive::Do\n"; }
};
struct Deride : Bass , ExtraBass<Deride> {
static void Do() { std::cout << "Deride::Do\n"; }
};
int main() {
Drive d1;
Deride d2;
Deride d3;
}
This is not an easy thing to do in C++, but I'm not saying it's impossible. If all you need is a list of subclass names, these answers might help:
Somehow register my classes in a list
c++ List of classes without initializing them for use of static functions
Seems either macro magic or boost mpl is your tool of choice.
I just wondering, if you did something like
void SomeFunc(void (*doFunc)())
{
doFunc();
}
template <class T> int Register()
{
SomeFunc(T::Do);
return 0;
}
template <class T> class Base
{
static int _i;
};
template <class T> int Base<T>::_i = Register<T>();
class Derived : Base<Derived>
{
public:
static void Do() { }
};

How can be done this automatic?

Is there any way to do this automatically for all derived classes, that i don't have to create function applyPack for all nested classes.
This is piece of my code:
/** every class has registered id with this function */
template<typename T>
uint getID() {
static uint id = registerClass<T>();
return id;
}
class TemplatesPack {
public:
template<typename T>
typename T::Template get();
};
class Object {
public:
virtual void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object>()); };
};
class Object2: public Object {
public:
void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object2>()); };
};
class Object3: public Object {
public:
void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object3>()); };
};
class Object4: public Object2 {
public:
void applyPack(TemplatesPack *p) { this->setTemplate(p->get<Object4>()); };
};
I've read something about type traits but i don't want to have class Object templated. Can be this done with c++ and templating some functions in class TemplatesPack or with c++0x? s
Edit:
changed the answer to make the Object untouched.
template<class T>
class Base<T> : public Object
{
public:
virtual void applyPack(TemplatePack *p) { this->setTemplate(p->get<T>()); };
};
class Object2 : public Base<Object2>
{
// ...
};
Edit: for the case of Object4, maybe the following will help:
template<class S, class D>
class Base<S, D> : public S
{
public:
virtual void applyPack(TemplatePack *p) { this->setTemplate(p->get<D>()); };
};
class Object2 : public Base<Object, Object2> { /* ... */ };
class Object3 : public Base<Object, Object3> { /* ... */ };
class Object4 : public Base<Object2, Object4> { /* ... */ };
You could use virtual inheritance and the dominance rule, if you don't want to templatize Base
template<typename Derived, typename Base = void>
struct applyer : virtual applyer<Base, typename Base::base_type> {
virtual void applyPack(TemplatesPack *p) {
dynamic_cast<Derived*>(this)->setTemplate(p->get<Derived>());
};
typedef Base base_type;
};
template<typename Derived>
struct applyer<Derived, void> {
virtual void applyPack(TemplatesPack *p) {
dynamic_cast<Derived*>(this)->setTemplate(p->get<Derived>());
};
};
Now you can do it as follows
class Object : virtual public applyer<Object> {
};
class Object2: public Object, virtual public applyer<Object2, Object> {
};
class Object3: public Object, virtual public applyer<Object3, Object> {
};
The second argument respectively is the direct base class, which can be omitted if there is none. For instance if you derive from Object3, you need to do that as follows
class Object3_1: public Object3, virtual public applyer<Object3_1, Object3> {
};