I have a usecase for two different structs (just storage of data) which share some common members, e.g.
struct Foo {
int i;
int j;
};
struct Bar {
int i;
float f;
};
The common data could be represented in a base struct but I'd like to disable the possibility of creating an object of the base struct.
CodePudding user response:
If you make the constructor protected, then only derived classes can access it:
struct Base {
int i;
protected:
Base() = default;
};
CodePudding user response:
One can artificially introduce an abstract method in the base class, e.g.
struct Base {
int i;
protected:
virtual void blockMe() const = 0;
};
struct Foo : public Base {
int j;
private:
void blockMe() const final {}
};
struct Bar : public Base {
float f;
private:
void blockMe() const final {}
};