C++ enforcing implementing a pure virtual method from a different inheritance hierarchy
I have a Parent class and two Child classes:
class Parent
{
};
class Child1: public Parent
{
};
class Child2 : public Parent
{
};
I would like a second class hierarchy, with class Cousin at the top of the hierarchy and subclasses called Cousin1, Cousin2 etc:
class Cousin
{
virtual void doUsefulWork() = 0;
};
class Cousin1 : public Cousin
{
virtual void doUsefulWork() override
{
// One behaviour
}
};
class Cousin2 : public Cousin
{
virtual void doUsefulWork() override
{
// A different behaviour
}
};
Each Child subclass MUST inherit a Cousin subclass, to have an implementation of doUsefulWork():
class Child1 : public Parent, Cousin1
{
// Has implementation of doUsefulWork() from Cousin1
};
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
Every Child subclass must have access to an implementation of doUsefulWork().
I was thinking of making Cousin inherit from Parent and defining the pure virtual doUsefulWork() in Parent, but then I would have complicated multiple inheritance issues in Child subclasses?
c++ inheritance polymorphism multiple-inheritance pure-virtual
|
show 9 more comments
I have a Parent class and two Child classes:
class Parent
{
};
class Child1: public Parent
{
};
class Child2 : public Parent
{
};
I would like a second class hierarchy, with class Cousin at the top of the hierarchy and subclasses called Cousin1, Cousin2 etc:
class Cousin
{
virtual void doUsefulWork() = 0;
};
class Cousin1 : public Cousin
{
virtual void doUsefulWork() override
{
// One behaviour
}
};
class Cousin2 : public Cousin
{
virtual void doUsefulWork() override
{
// A different behaviour
}
};
Each Child subclass MUST inherit a Cousin subclass, to have an implementation of doUsefulWork():
class Child1 : public Parent, Cousin1
{
// Has implementation of doUsefulWork() from Cousin1
};
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
Every Child subclass must have access to an implementation of doUsefulWork().
I was thinking of making Cousin inherit from Parent and defining the pure virtual doUsefulWork() in Parent, but then I would have complicated multiple inheritance issues in Child subclasses?
c++ inheritance polymorphism multiple-inheritance pure-virtual
I would do it the other way around and inheritParentfromCousin. This will at least generate a compile error if an author of aChildclass forgets to reimplementdoUsefulWork()or inherit from a class that has an implementation.
– dave
Nov 28 '18 at 13:28
Or vice versa,class Parent : public Cousin { ...will do the trick.
– Eljay
Nov 28 '18 at 13:28
2
i hope the names are just for the example here. Imho the "child-parent" analogon is one of the worst, asstruct Child : Parentactually means that everyChildis-aParent, no wonder that it causes confusion when OO is thaught like that
– user463035818
Nov 28 '18 at 13:28
I'm not sure whetherclass Parent: public Cousin {is such a good idea. Derivingclass Child1: public Parent, Cousin1 {would have the base classCousintwice (and that smells like the need of virtual inheritance...)
– Scheff
Nov 28 '18 at 13:32
What I don't understand:Child1accessed with a pointer toParentmakes the virtual methoddoUsefulWork()invisible. It's a pointer toCousinwhich is needed. If in turn,Child3is accidentally not derived from anyCousinXwith overriddendoUsefulWork(), assigning it to a pointer toCousinshould cause trouble. There is something subtle, I'm not yet aware of.
– Scheff
Nov 28 '18 at 13:37
|
show 9 more comments
I have a Parent class and two Child classes:
class Parent
{
};
class Child1: public Parent
{
};
class Child2 : public Parent
{
};
I would like a second class hierarchy, with class Cousin at the top of the hierarchy and subclasses called Cousin1, Cousin2 etc:
class Cousin
{
virtual void doUsefulWork() = 0;
};
class Cousin1 : public Cousin
{
virtual void doUsefulWork() override
{
// One behaviour
}
};
class Cousin2 : public Cousin
{
virtual void doUsefulWork() override
{
// A different behaviour
}
};
Each Child subclass MUST inherit a Cousin subclass, to have an implementation of doUsefulWork():
class Child1 : public Parent, Cousin1
{
// Has implementation of doUsefulWork() from Cousin1
};
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
Every Child subclass must have access to an implementation of doUsefulWork().
I was thinking of making Cousin inherit from Parent and defining the pure virtual doUsefulWork() in Parent, but then I would have complicated multiple inheritance issues in Child subclasses?
c++ inheritance polymorphism multiple-inheritance pure-virtual
I have a Parent class and two Child classes:
class Parent
{
};
class Child1: public Parent
{
};
class Child2 : public Parent
{
};
I would like a second class hierarchy, with class Cousin at the top of the hierarchy and subclasses called Cousin1, Cousin2 etc:
class Cousin
{
virtual void doUsefulWork() = 0;
};
class Cousin1 : public Cousin
{
virtual void doUsefulWork() override
{
// One behaviour
}
};
class Cousin2 : public Cousin
{
virtual void doUsefulWork() override
{
// A different behaviour
}
};
Each Child subclass MUST inherit a Cousin subclass, to have an implementation of doUsefulWork():
class Child1 : public Parent, Cousin1
{
// Has implementation of doUsefulWork() from Cousin1
};
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
Every Child subclass must have access to an implementation of doUsefulWork().
I was thinking of making Cousin inherit from Parent and defining the pure virtual doUsefulWork() in Parent, but then I would have complicated multiple inheritance issues in Child subclasses?
c++ inheritance polymorphism multiple-inheritance pure-virtual
c++ inheritance polymorphism multiple-inheritance pure-virtual
edited Nov 28 '18 at 13:31
user997112
asked Nov 28 '18 at 13:23
user997112user997112
10.1k28106220
10.1k28106220
I would do it the other way around and inheritParentfromCousin. This will at least generate a compile error if an author of aChildclass forgets to reimplementdoUsefulWork()or inherit from a class that has an implementation.
– dave
Nov 28 '18 at 13:28
Or vice versa,class Parent : public Cousin { ...will do the trick.
– Eljay
Nov 28 '18 at 13:28
2
i hope the names are just for the example here. Imho the "child-parent" analogon is one of the worst, asstruct Child : Parentactually means that everyChildis-aParent, no wonder that it causes confusion when OO is thaught like that
– user463035818
Nov 28 '18 at 13:28
I'm not sure whetherclass Parent: public Cousin {is such a good idea. Derivingclass Child1: public Parent, Cousin1 {would have the base classCousintwice (and that smells like the need of virtual inheritance...)
– Scheff
Nov 28 '18 at 13:32
What I don't understand:Child1accessed with a pointer toParentmakes the virtual methoddoUsefulWork()invisible. It's a pointer toCousinwhich is needed. If in turn,Child3is accidentally not derived from anyCousinXwith overriddendoUsefulWork(), assigning it to a pointer toCousinshould cause trouble. There is something subtle, I'm not yet aware of.
– Scheff
Nov 28 '18 at 13:37
|
show 9 more comments
I would do it the other way around and inheritParentfromCousin. This will at least generate a compile error if an author of aChildclass forgets to reimplementdoUsefulWork()or inherit from a class that has an implementation.
– dave
Nov 28 '18 at 13:28
Or vice versa,class Parent : public Cousin { ...will do the trick.
– Eljay
Nov 28 '18 at 13:28
2
i hope the names are just for the example here. Imho the "child-parent" analogon is one of the worst, asstruct Child : Parentactually means that everyChildis-aParent, no wonder that it causes confusion when OO is thaught like that
– user463035818
Nov 28 '18 at 13:28
I'm not sure whetherclass Parent: public Cousin {is such a good idea. Derivingclass Child1: public Parent, Cousin1 {would have the base classCousintwice (and that smells like the need of virtual inheritance...)
– Scheff
Nov 28 '18 at 13:32
What I don't understand:Child1accessed with a pointer toParentmakes the virtual methoddoUsefulWork()invisible. It's a pointer toCousinwhich is needed. If in turn,Child3is accidentally not derived from anyCousinXwith overriddendoUsefulWork(), assigning it to a pointer toCousinshould cause trouble. There is something subtle, I'm not yet aware of.
– Scheff
Nov 28 '18 at 13:37
I would do it the other way around and inherit
Parent from Cousin. This will at least generate a compile error if an author of a Child class forgets to reimplement doUsefulWork() or inherit from a class that has an implementation.– dave
Nov 28 '18 at 13:28
I would do it the other way around and inherit
Parent from Cousin. This will at least generate a compile error if an author of a Child class forgets to reimplement doUsefulWork() or inherit from a class that has an implementation.– dave
Nov 28 '18 at 13:28
Or vice versa,
class Parent : public Cousin { ... will do the trick.– Eljay
Nov 28 '18 at 13:28
Or vice versa,
class Parent : public Cousin { ... will do the trick.– Eljay
Nov 28 '18 at 13:28
2
2
i hope the names are just for the example here. Imho the "child-parent" analogon is one of the worst, as
struct Child : Parent actually means that every Child is-a Parent, no wonder that it causes confusion when OO is thaught like that– user463035818
Nov 28 '18 at 13:28
i hope the names are just for the example here. Imho the "child-parent" analogon is one of the worst, as
struct Child : Parent actually means that every Child is-a Parent, no wonder that it causes confusion when OO is thaught like that– user463035818
Nov 28 '18 at 13:28
I'm not sure whether
class Parent: public Cousin { is such a good idea. Deriving class Child1: public Parent, Cousin1 { would have the base class Cousin twice (and that smells like the need of virtual inheritance...)– Scheff
Nov 28 '18 at 13:32
I'm not sure whether
class Parent: public Cousin { is such a good idea. Deriving class Child1: public Parent, Cousin1 { would have the base class Cousin twice (and that smells like the need of virtual inheritance...)– Scheff
Nov 28 '18 at 13:32
What I don't understand:
Child1 accessed with a pointer to Parent makes the virtual method doUsefulWork() invisible. It's a pointer to Cousin which is needed. If in turn, Child3 is accidentally not derived from any CousinX with overridden doUsefulWork(), assigning it to a pointer to Cousin should cause trouble. There is something subtle, I'm not yet aware of.– Scheff
Nov 28 '18 at 13:37
What I don't understand:
Child1 accessed with a pointer to Parent makes the virtual method doUsefulWork() invisible. It's a pointer to Cousin which is needed. If in turn, Child3 is accidentally not derived from any CousinX with overridden doUsefulWork(), assigning it to a pointer to Cousin should cause trouble. There is something subtle, I'm not yet aware of.– Scheff
Nov 28 '18 at 13:37
|
show 9 more comments
1 Answer
1
active
oldest
votes
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
The reason you are implementing these interfaces is because there must be functions that consume these interfaces. If an object doesn't implement the interface you get a compiler error when passing the object to that function - that acts as an enforcing check that the object must implement that interface.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520462%2fc-enforcing-implementing-a-pure-virtual-method-from-a-different-inheritance-hi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
The reason you are implementing these interfaces is because there must be functions that consume these interfaces. If an object doesn't implement the interface you get a compiler error when passing the object to that function - that acts as an enforcing check that the object must implement that interface.
add a comment |
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
The reason you are implementing these interfaces is because there must be functions that consume these interfaces. If an object doesn't implement the interface you get a compiler error when passing the object to that function - that acts as an enforcing check that the object must implement that interface.
add a comment |
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
The reason you are implementing these interfaces is because there must be functions that consume these interfaces. If an object doesn't implement the interface you get a compiler error when passing the object to that function - that acts as an enforcing check that the object must implement that interface.
How can I guard against the author of a Child subclass forgetting to inherit a Cousin subclass? I would like something which generates a compile time error.
The reason you are implementing these interfaces is because there must be functions that consume these interfaces. If an object doesn't implement the interface you get a compiler error when passing the object to that function - that acts as an enforcing check that the object must implement that interface.
answered Nov 28 '18 at 13:43
Maxim EgorushkinMaxim Egorushkin
89.2k11104191
89.2k11104191
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520462%2fc-enforcing-implementing-a-pure-virtual-method-from-a-different-inheritance-hi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I would do it the other way around and inherit
ParentfromCousin. This will at least generate a compile error if an author of aChildclass forgets to reimplementdoUsefulWork()or inherit from a class that has an implementation.– dave
Nov 28 '18 at 13:28
Or vice versa,
class Parent : public Cousin { ...will do the trick.– Eljay
Nov 28 '18 at 13:28
2
i hope the names are just for the example here. Imho the "child-parent" analogon is one of the worst, as
struct Child : Parentactually means that everyChildis-aParent, no wonder that it causes confusion when OO is thaught like that– user463035818
Nov 28 '18 at 13:28
I'm not sure whether
class Parent: public Cousin {is such a good idea. Derivingclass Child1: public Parent, Cousin1 {would have the base classCousintwice (and that smells like the need of virtual inheritance...)– Scheff
Nov 28 '18 at 13:32
What I don't understand:
Child1accessed with a pointer toParentmakes the virtual methoddoUsefulWork()invisible. It's a pointer toCousinwhich is needed. If in turn,Child3is accidentally not derived from anyCousinXwith overriddendoUsefulWork(), assigning it to a pointer toCousinshould cause trouble. There is something subtle, I'm not yet aware of.– Scheff
Nov 28 '18 at 13:37