Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

You're right, this is a flaw in the language itself which means that despite all the advantages which come from inheritance, you still have to know how it is implemented. It would be ideal to be able to call super within the method and have it call the appropriate method, whatever that might be. This is one of the things that Java tried to improve upon.

As you said, you could get around it by implementing every method in class A, and so in doing so, you prevent complications later on. It is a bit of a pain, but I suppose it is a necessary evil for creating a proper library to be used by others. Note that only the class being made available in the library for use has to implement all virtual methods, and not any internally used classes unless you require otherwise.

While not a solution, a slightly friendlier approach (albeit a little hacky) might be to typedef "super" as a private type definition for the base class. As in the case of class B, you'd have:

private: typedef A super; 

Likewise in class A, you could define:

private: typedef Base super; 

At least if you're the one writing the code, rather than have to know the name of the class, you could do:

class B : A { void foo(bool param) override { // do some stuff here. super::foo(param); } void foo1(int param) override { super::super::foo1(1); } }; 

Again, while this doesn't fix your problem, it makes it slightly more manageable.

See here for more details.See here for more details.

You're right, this is a flaw in the language itself which means that despite all the advantages which come from inheritance, you still have to know how it is implemented. It would be ideal to be able to call super within the method and have it call the appropriate method, whatever that might be. This is one of the things that Java tried to improve upon.

As you said, you could get around it by implementing every method in class A, and so in doing so, you prevent complications later on. It is a bit of a pain, but I suppose it is a necessary evil for creating a proper library to be used by others. Note that only the class being made available in the library for use has to implement all virtual methods, and not any internally used classes unless you require otherwise.

While not a solution, a slightly friendlier approach (albeit a little hacky) might be to typedef "super" as a private type definition for the base class. As in the case of class B, you'd have:

private: typedef A super; 

Likewise in class A, you could define:

private: typedef Base super; 

At least if you're the one writing the code, rather than have to know the name of the class, you could do:

class B : A { void foo(bool param) override { // do some stuff here. super::foo(param); } void foo1(int param) override { super::super::foo1(1); } }; 

Again, while this doesn't fix your problem, it makes it slightly more manageable.

See here for more details.

You're right, this is a flaw in the language itself which means that despite all the advantages which come from inheritance, you still have to know how it is implemented. It would be ideal to be able to call super within the method and have it call the appropriate method, whatever that might be. This is one of the things that Java tried to improve upon.

As you said, you could get around it by implementing every method in class A, and so in doing so, you prevent complications later on. It is a bit of a pain, but I suppose it is a necessary evil for creating a proper library to be used by others. Note that only the class being made available in the library for use has to implement all virtual methods, and not any internally used classes unless you require otherwise.

While not a solution, a slightly friendlier approach (albeit a little hacky) might be to typedef "super" as a private type definition for the base class. As in the case of class B, you'd have:

private: typedef A super; 

Likewise in class A, you could define:

private: typedef Base super; 

At least if you're the one writing the code, rather than have to know the name of the class, you could do:

class B : A { void foo(bool param) override { // do some stuff here. super::foo(param); } void foo1(int param) override { super::super::foo1(1); } }; 

Again, while this doesn't fix your problem, it makes it slightly more manageable.

See here for more details.

Source Link
Neil
  • 22.9k
  • 48
  • 76

You're right, this is a flaw in the language itself which means that despite all the advantages which come from inheritance, you still have to know how it is implemented. It would be ideal to be able to call super within the method and have it call the appropriate method, whatever that might be. This is one of the things that Java tried to improve upon.

As you said, you could get around it by implementing every method in class A, and so in doing so, you prevent complications later on. It is a bit of a pain, but I suppose it is a necessary evil for creating a proper library to be used by others. Note that only the class being made available in the library for use has to implement all virtual methods, and not any internally used classes unless you require otherwise.

While not a solution, a slightly friendlier approach (albeit a little hacky) might be to typedef "super" as a private type definition for the base class. As in the case of class B, you'd have:

private: typedef A super; 

Likewise in class A, you could define:

private: typedef Base super; 

At least if you're the one writing the code, rather than have to know the name of the class, you could do:

class B : A { void foo(bool param) override { // do some stuff here. super::foo(param); } void foo1(int param) override { super::super::foo1(1); } }; 

Again, while this doesn't fix your problem, it makes it slightly more manageable.

See here for more details.