C++ enforcing implementing a pure virtual method from a different inheritance hierarchy












0















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?










share|improve this question

























  • 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






  • 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'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


















0















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?










share|improve this question

























  • 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






  • 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'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
















0












0








0


1






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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






  • 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'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





















  • 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






  • 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'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



















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














1 Answer
1






active

oldest

votes


















0















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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0















    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.






    share|improve this answer




























      0















      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.






      share|improve this answer


























        0












        0








        0








        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.






        share|improve this answer














        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 13:43









        Maxim EgorushkinMaxim Egorushkin

        89.2k11104191




        89.2k11104191
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks