N
nguillot
Hello.
Imagine I have the following class hierarchy:
class A
{ ... };
class B : public virtual class A
{ ... };
class C : public virtual class A
{ ... };
class Concrete : public B, public C
{ ... };
class A is an interface of all concrete objects of type A.
These concrete objects can inherit from several interfaces (here, B
and C), that inherits from A (virtual inheritance).
If all object of type A necessarily have a property (let's say an id),
they can't exist without.
I'd like to store this id in the virtual base:
class A
{
int m_id;
public:
A() {}
A(int p_id) : m_id(p_id) {}
virtual ~A() {}
int GetId() const { return m_id; }
};
class B : public virtual A
{
public:
B() {}
virtual ~B() {}
};
class C : public virtual A
{
public:
C() {}
virtual ~C() {}
};
class Concrete : public B, public C
{
public:
Concrete(int p_id) : A(p_id) {}
virtual ~Concrete() {}
};
It's ok: the users of the class Concrete are only able to create a
Concrete object passing the id.
Now the question: developers have access to Concrete code, but not to
A, B nor C.
How could I forbid them (by compilation error) to add another ctor in
Concrete?
I would expect something like removing the A() {}, but is is needed to
instantiate B and C.
Thanks in advance.
Imagine I have the following class hierarchy:
class A
{ ... };
class B : public virtual class A
{ ... };
class C : public virtual class A
{ ... };
class Concrete : public B, public C
{ ... };
class A is an interface of all concrete objects of type A.
These concrete objects can inherit from several interfaces (here, B
and C), that inherits from A (virtual inheritance).
If all object of type A necessarily have a property (let's say an id),
they can't exist without.
I'd like to store this id in the virtual base:
class A
{
int m_id;
public:
A() {}
A(int p_id) : m_id(p_id) {}
virtual ~A() {}
int GetId() const { return m_id; }
};
class B : public virtual A
{
public:
B() {}
virtual ~B() {}
};
class C : public virtual A
{
public:
C() {}
virtual ~C() {}
};
class Concrete : public B, public C
{
public:
Concrete(int p_id) : A(p_id) {}
virtual ~Concrete() {}
};
It's ok: the users of the class Concrete are only able to create a
Concrete object passing the id.
Now the question: developers have access to Concrete code, but not to
A, B nor C.
How could I forbid them (by compilation error) to add another ctor in
Concrete?
I would expect something like removing the A() {}, but is is needed to
instantiate B and C.
Thanks in advance.