how to be sure each child class inherits a method?
-
I have a straight class hierarchy. The base class only has a method F() which calls another method P(). This method P() has to be implemented in all of the child classes and is called by polymorphism. How can I get sure (at compile time) that all child classes implement this method P()?
class CBase
{
private:
virtual void P() { printf("CBase::P\n"); }
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase
{
private:
virtual void P() override { printf("C1::P\n"); }
};
class C2 : public C1
{
private:
virtual void P() override { printf("C2::P\n"); }
};void main()
{
CBase* p = new CBase();
p->F();
p = new C1();
p->F();
p = new C2();
p->F();
}The above is the structure, when all is correct and will result in: Base: F()->P(): CBase::P Base: F()->P(): C1::P Base: F()->P(): C2::P Now I made a mistake and forgot to implement P() in class C1
class C1 : public CBase
{
private:
//virtual void P() override { printf("C1::P\n"); }
};the compiler doesn´t error and it will result in Base: F()->P(): CBase::P Base: F()->P(): CBase::P Base: F()->P(): C2::P The only Idea is to use an Interface declaring P() abstract. But since C2 is derived from C1 from CBase how to do? Followeing code will show up an error on missing P(), but also shows up warnings when all is correct: _warning C4584: 'C1': base class 'PI' is already base of 'CBase' warning C4584: 'C2': base class 'PI' is already base of 'C1'
struct PI
{
virtual void P() abstract;
};class CBase : public PI
{
private:
virtual void P() { printf("CBase::P\n"); }
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase, public PI
{
private:
virtual void P() override { printf("C1::P\n"); }
};
class C2 : public C1, public PI
{
private:
virtual void P() override { printf("C2::P\n"); }
};also it doesnt help at all because now I have to be sure to derive C1,C2, ... from PI too, not only from its parent So what I need is "method P() has to be declared in all child classes!- error if it is not"_
-
I have a straight class hierarchy. The base class only has a method F() which calls another method P(). This method P() has to be implemented in all of the child classes and is called by polymorphism. How can I get sure (at compile time) that all child classes implement this method P()?
class CBase
{
private:
virtual void P() { printf("CBase::P\n"); }
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase
{
private:
virtual void P() override { printf("C1::P\n"); }
};
class C2 : public C1
{
private:
virtual void P() override { printf("C2::P\n"); }
};void main()
{
CBase* p = new CBase();
p->F();
p = new C1();
p->F();
p = new C2();
p->F();
}The above is the structure, when all is correct and will result in: Base: F()->P(): CBase::P Base: F()->P(): C1::P Base: F()->P(): C2::P Now I made a mistake and forgot to implement P() in class C1
class C1 : public CBase
{
private:
//virtual void P() override { printf("C1::P\n"); }
};the compiler doesn´t error and it will result in Base: F()->P(): CBase::P Base: F()->P(): CBase::P Base: F()->P(): C2::P The only Idea is to use an Interface declaring P() abstract. But since C2 is derived from C1 from CBase how to do? Followeing code will show up an error on missing P(), but also shows up warnings when all is correct: _warning C4584: 'C1': base class 'PI' is already base of 'CBase' warning C4584: 'C2': base class 'PI' is already base of 'C1'
struct PI
{
virtual void P() abstract;
};class CBase : public PI
{
private:
virtual void P() { printf("CBase::P\n"); }
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase, public PI
{
private:
virtual void P() override { printf("C1::P\n"); }
};
class C2 : public C1, public PI
{
private:
virtual void P() override { printf("C2::P\n"); }
};also it doesnt help at all because now I have to be sure to derive C1,C2, ... from PI too, not only from its parent So what I need is "method P() has to be declared in all child classes!- error if it is not"_
-
If you make P a pure virtual function, then the compiler will spot if it has not been implemented:
virtual void P() = 0; // a pure virtual function
in which class, where to put this? in CBase? -> then CBase doesnt have its P() method itself in struct PI? ->still used this "=0" is same systax like "abstract"
-
in which class, where to put this? in CBase? -> then CBase doesnt have its P() method itself in struct PI? ->still used this "=0" is same systax like "abstract"
-
does not work ... I can ommit P() in C2 without compile error because its stil defined in C1 also I miss the code of CBase::P() itself like:
class CBase
{
private:
virtual void P() abstract; //{ printf("CBase::P\n"); } // <= and whats about that code??
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase
{
private:
void P() override { printf("C1::P\n"); }
};
class C2 : public C1
{
private:
//void P() override { printf("C2::P\n"); }
}; -
does not work ... I can ommit P() in C2 without compile error because its stil defined in C1 also I miss the code of CBase::P() itself like:
class CBase
{
private:
virtual void P() abstract; //{ printf("CBase::P\n"); } // <= and whats about that code??
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase
{
private:
void P() override { printf("C1::P\n"); }
};
class C2 : public C1
{
private:
//void P() override { printf("C2::P\n"); }
}; -
The function
C1::P()
is declared private so it is not accessible in classC2
. However, it is difficult to see exactly what problem you are trying to solve here.The structure is: CBase <- C1 <- C2 <- ... etc so straigt inheritance CBase contains the main method CBase::F() which does all calculations needed. And this method F() calls P() which should be choosen by inheritance (polymorphism) CBase::F() { ...; P(); ... } If I have CBase* b = new C2() than C2::P() will be called. If I have CBase* b = new C1() than C1::P() will be called. because of P() is a virtual method, overridden by C1::P().. C2::P() etc now I miss C1::P() (forgot to add it) => if CBase b=new C1() then CBase::F() will call CBase::P() instead of C1::P() because there is no C1::P() the override clause also tells the compiler to check if C1::P(), C2::P() match their parents P(). All is cointained by a complex chaotic SW-project which I will clean by small steps. The idea behind: P() is a call to a factory class which creates objects dependend on parameters AND on the class from which is called CBase::P(int type) creates objects of CPbase_ so CPbase_1, CPbase_2... C1::P(int type) creates obejcts of CP1_ so CP1_1, CP1_2... C2::P(int type) creates obejcts of CP2_ so CP2_1, CP2_2... etc... CBase::F(switch,type) : if (switch) P(type) else CBase::P(type) ... So if switch is set call the factory "of the class", else call the factory of CBase example: Cbase b = new C1(); b->F(true, 3) will create an object of C1_3() All the pointers are stored in a list and for each element in the list the creation-funbtion F() is called list ... C1*,CBase*,C3* etc ... foreach element in list , element->F(switch,type) ...
-
I have a straight class hierarchy. The base class only has a method F() which calls another method P(). This method P() has to be implemented in all of the child classes and is called by polymorphism. How can I get sure (at compile time) that all child classes implement this method P()?
class CBase
{
private:
virtual void P() { printf("CBase::P\n"); }
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase
{
private:
virtual void P() override { printf("C1::P\n"); }
};
class C2 : public C1
{
private:
virtual void P() override { printf("C2::P\n"); }
};void main()
{
CBase* p = new CBase();
p->F();
p = new C1();
p->F();
p = new C2();
p->F();
}The above is the structure, when all is correct and will result in: Base: F()->P(): CBase::P Base: F()->P(): C1::P Base: F()->P(): C2::P Now I made a mistake and forgot to implement P() in class C1
class C1 : public CBase
{
private:
//virtual void P() override { printf("C1::P\n"); }
};the compiler doesn´t error and it will result in Base: F()->P(): CBase::P Base: F()->P(): CBase::P Base: F()->P(): C2::P The only Idea is to use an Interface declaring P() abstract. But since C2 is derived from C1 from CBase how to do? Followeing code will show up an error on missing P(), but also shows up warnings when all is correct: _warning C4584: 'C1': base class 'PI' is already base of 'CBase' warning C4584: 'C2': base class 'PI' is already base of 'C1'
struct PI
{
virtual void P() abstract;
};class CBase : public PI
{
private:
virtual void P() { printf("CBase::P\n"); }
public:
void F() { printf("Base: F()->P(): "), P(); }
};class C1 : public CBase, public PI
{
private:
virtual void P() override { printf("C1::P\n"); }
};
class C2 : public C1, public PI
{
private:
virtual void P() override { printf("C2::P\n"); }
};also it doesnt help at all because now I have to be sure to derive C1,C2, ... from PI too, not only from its parent So what I need is "method P() has to be declared in all child classes!- error if it is not"_