C++ class Design with Inheritance - c++

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>;

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.

Pure virtual method that returns children of an abstract class?

I'm trying to make an abstract class, and one of the methods that its children must override should return an instance of the child class.
class JsonSerializable {
public:
virtual <child_of_JsonSerializable> fromJson(string jsonStr) const = 0;
};
class ConcreteSerializable : public JsonSerializable {
public:
ConcreteSerializable fromJson(string jsonStr) const {
return ConcreteSerializable();
}
};
I tried using templates following this answer, but I get an error that templates may not be virtual.
Is there a way to do what I'm looking for without using raw pointers as the return type?
You cannot create an object of an abstract type. And because you cannot create such an object, you also cannot return it. This is the reason why all examples returning a base/derived object, always return a pointer or some reference to the base class
struct B {
virtual B *fromJson(const std::string &jsonStr) const = 0;
};
struct D : public B {
D(const std::string &jsonStr);
D *fromJson(const std::string &jsonStr) const;
};
D *D::fromJson(const std::string &jsonStr) const
{
return new D(jsonStr);
}
Are you trying to implement something that historically was done using CRTP?
struct Interface {
virtual Interface *inflate(std::string const &json) = 0;
virtual ~Interface() {}
};
template<typename Child> struct Base: public Interface {
Interface *inflate(std::string const &json) { return new Child(json); }
};
struct Child: public Base<Child> {
Child(std::string const &json);
};

Exposing base class enum without tediously listing enumerators

In C++11, I can expose an enumerator which is protected in a base class, to users of a derived class, as follows:
class Base
{
protected:
enum Waldo { hidden, found };
};
class Derived : public Base
{
public:
using Base::Waldo;
}
void foo()
{
Derived::Waldo w = Derived::Waldo::found;
}
Unfortunately, in C++03, Derived::Waldo::found is illegal, and Derived::found is met with 'Base::Waldo Base::found' is protected within this context.
I could work around that by also writing a using for each enumerator:
class Derived : public Base
{
public:
using Base::Waldo;
using Base::hidden;
using Base::found;
}
void foo()
{
Derived::Waldo w = Derived::found; // works in C++03
}
but this can be really tedious to do if the enumerator has many enumerators. Is there a way to pull off this enum-exposing in C++03 without this tedium?
SCNR!
struct Wrap
{
enum Waldo { hidden, found };
};
class Base : protected Wrap
{
};
class Derived : public Base
{
public:
using Base::Wrap;
};
void foo()
{
Derived::Wrap::Waldo w = Derived::Wrap::found;
}
Edit: Or you put it inside:
class Base
{
protected:
struct Wrap
{
enum Waldo { hidden, found };
};
};
class Derived : public Base
{
public:
using Base::Wrap;
};
void foo()
{
Derived::Wrap::Waldo w = Derived::Wrap::found;
}
Well, I think there are lots of different ways to do this, depending on what you want to do with the enum. Here is one:
struct WaldoStates
{
enum States{hidden, found};
};
class Base
{
public:
typedef WaldoStates Waldo;
};
class Derived : public Base
{
};
void foo()
{
WaldoStates::States state = Derived::Waldo::found;
}
Now, this is good if simply want the states to be accessible with something like myClass::Waldo::hidden in any class of the inheritance tree. If you want to be able to set different states for some of the derived classes, you may want to do something like this instead:
struct WaldoStates
{
typedef int State;
};
struct DefaultWaldoStates : public WaldoStates
{
enum States{hidden, found};
};
struct OtherWaldoStates : public WaldoStates
{
enum States{stillHidden, alreadyFound};
};
class Base
{
public:
typedef DefaultWaldoStates Waldo;
};
class Derived : public Base
{
};
class OtherDerived : public Base
{
public:
typedef OtherWaldoStates Waldo;
};
void foo()
{
WaldoStates::State state0 = Derived::Waldo::found;
WaldoStates::State state1 = OtherDerived::Waldo::alreadyFound;
}

Statically Init a derived class

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.
}
}

Multiple inheritance hierarchy

I'm looking for a clean way of doing this since a long time. In my problem, there exist 3 classes not sharing any parent in common but each having some methods with the same name (A.doSomething, B.doSomething, C.doSomething). Hence, having the same function signature, class D inheriting from A and using method doSomething() will "look the same" to E inheriting from B or C .
Here is a sketch of what I'd like to be able to do:
class Base {
public:
void myMethod(void) { doSomething(); }
};
class Independent {
public:
doSomething();
};
clase Derived : public Base : public Independent {
(...)
};
int main(void) {
Derived *derivedObject = new Derived();
derivedObject->myMethod();
}
In this problem, object of type "Independent" is provided by a library that I cannot change. I would like to define a base class that uses methods that are going to be inherited later on. I couldn't find a proper way of doing this using virtual inheritance without causing ambiguous compiling.
You've got a nasty situation there. One solution to this would be using the Curiously Recurring Template Pattern to perform the inheritance at compile-time, like this:
template <typename D>
class Base {
public:
void myMethod(void) { static_cast<D*>(this)->doSomething(); }
};
class Independent {
public:
void doSomething();
};
clase Derived : public Base : public Independent {
/*...*/
};
int main(void) {
Derived *derivedObject = new Derived();
derivedObject->myMethod();
}
Alternatively, you could choose to put a middleman class in between to forward to Independent (I assume you have many classes deriving from the same Base and Independent, and just don't want to have to do this for each class).
template <typename D>
class Base {
private:
virtual void doSomethingImpl();
public:
void myMethod(void) { doSomethingImpl(); }
};
class Independent {
public:
void doSomething();
};
class IndependentWrapper : public Base : public Independent {
private:
void doSomethingImpl() { Independent::doSomething(); }
};
clase Derived : public IndependentWrapper {
/*...*/
};
int main(void) {
Derived *derivedObject = new Derived();
derivedObject->myMethod();
}