Where to use the override keyword in C++

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












Which is an ideal file to use override identifier in c++ .h or .cpp?



war.h



class TimeStone : public InfinityStone

private:
bool capturedByThanos() override; #1
;


war.cpp



bool TimeStone::capturedByThanos() override #2

return true;










share|improve this question



















  • 2




    In the header is surely enough but how do you override a non virtual private method?
    – Adriano Repetti
    2 hours ago







  • 3




    @AdrianoRepetti - How do you know it's non-virtual? Presumably InfinityStone defines it as virtual.
    – StoryTeller
    2 hours ago










  • @StoryTeller just an assumption, class isn't final, inheritance is public and the overridden method is private. It might well be that it's on purpose but given the question I assumed it's not (not that it might not be, anyway)
    – Adriano Repetti
    1 hour ago











  • @AdrianoRepetti there is nothing inherently wrong with OPs code other than being incomplete. Instead of speculating how it could be wrong you could ask for the missing code ;)
    – user463035818
    1 hour ago






  • 1




    I would say that the code as is perfectly exemplifies the utility of override. You don't need the base class definition before your eyes. The compiler will tell you if you got it wrong thanks to override.
    – StoryTeller
    23 mins ago















up vote
6
down vote

favorite












Which is an ideal file to use override identifier in c++ .h or .cpp?



war.h



class TimeStone : public InfinityStone

private:
bool capturedByThanos() override; #1
;


war.cpp



bool TimeStone::capturedByThanos() override #2

return true;










share|improve this question



















  • 2




    In the header is surely enough but how do you override a non virtual private method?
    – Adriano Repetti
    2 hours ago







  • 3




    @AdrianoRepetti - How do you know it's non-virtual? Presumably InfinityStone defines it as virtual.
    – StoryTeller
    2 hours ago










  • @StoryTeller just an assumption, class isn't final, inheritance is public and the overridden method is private. It might well be that it's on purpose but given the question I assumed it's not (not that it might not be, anyway)
    – Adriano Repetti
    1 hour ago











  • @AdrianoRepetti there is nothing inherently wrong with OPs code other than being incomplete. Instead of speculating how it could be wrong you could ask for the missing code ;)
    – user463035818
    1 hour ago






  • 1




    I would say that the code as is perfectly exemplifies the utility of override. You don't need the base class definition before your eyes. The compiler will tell you if you got it wrong thanks to override.
    – StoryTeller
    23 mins ago













up vote
6
down vote

favorite









up vote
6
down vote

favorite











Which is an ideal file to use override identifier in c++ .h or .cpp?



war.h



class TimeStone : public InfinityStone

private:
bool capturedByThanos() override; #1
;


war.cpp



bool TimeStone::capturedByThanos() override #2

return true;










share|improve this question















Which is an ideal file to use override identifier in c++ .h or .cpp?



war.h



class TimeStone : public InfinityStone

private:
bool capturedByThanos() override; #1
;


war.cpp



bool TimeStone::capturedByThanos() override #2

return true;







c++ c++11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









StoryTeller

87.6k12174243




87.6k12174243










asked 2 hours ago









shingote

515




515







  • 2




    In the header is surely enough but how do you override a non virtual private method?
    – Adriano Repetti
    2 hours ago







  • 3




    @AdrianoRepetti - How do you know it's non-virtual? Presumably InfinityStone defines it as virtual.
    – StoryTeller
    2 hours ago










  • @StoryTeller just an assumption, class isn't final, inheritance is public and the overridden method is private. It might well be that it's on purpose but given the question I assumed it's not (not that it might not be, anyway)
    – Adriano Repetti
    1 hour ago











  • @AdrianoRepetti there is nothing inherently wrong with OPs code other than being incomplete. Instead of speculating how it could be wrong you could ask for the missing code ;)
    – user463035818
    1 hour ago






  • 1




    I would say that the code as is perfectly exemplifies the utility of override. You don't need the base class definition before your eyes. The compiler will tell you if you got it wrong thanks to override.
    – StoryTeller
    23 mins ago













  • 2




    In the header is surely enough but how do you override a non virtual private method?
    – Adriano Repetti
    2 hours ago







  • 3




    @AdrianoRepetti - How do you know it's non-virtual? Presumably InfinityStone defines it as virtual.
    – StoryTeller
    2 hours ago










  • @StoryTeller just an assumption, class isn't final, inheritance is public and the overridden method is private. It might well be that it's on purpose but given the question I assumed it's not (not that it might not be, anyway)
    – Adriano Repetti
    1 hour ago











  • @AdrianoRepetti there is nothing inherently wrong with OPs code other than being incomplete. Instead of speculating how it could be wrong you could ask for the missing code ;)
    – user463035818
    1 hour ago






  • 1




    I would say that the code as is perfectly exemplifies the utility of override. You don't need the base class definition before your eyes. The compiler will tell you if you got it wrong thanks to override.
    – StoryTeller
    23 mins ago








2




2




In the header is surely enough but how do you override a non virtual private method?
– Adriano Repetti
2 hours ago





In the header is surely enough but how do you override a non virtual private method?
– Adriano Repetti
2 hours ago





3




3




@AdrianoRepetti - How do you know it's non-virtual? Presumably InfinityStone defines it as virtual.
– StoryTeller
2 hours ago




@AdrianoRepetti - How do you know it's non-virtual? Presumably InfinityStone defines it as virtual.
– StoryTeller
2 hours ago












@StoryTeller just an assumption, class isn't final, inheritance is public and the overridden method is private. It might well be that it's on purpose but given the question I assumed it's not (not that it might not be, anyway)
– Adriano Repetti
1 hour ago





@StoryTeller just an assumption, class isn't final, inheritance is public and the overridden method is private. It might well be that it's on purpose but given the question I assumed it's not (not that it might not be, anyway)
– Adriano Repetti
1 hour ago













@AdrianoRepetti there is nothing inherently wrong with OPs code other than being incomplete. Instead of speculating how it could be wrong you could ask for the missing code ;)
– user463035818
1 hour ago




@AdrianoRepetti there is nothing inherently wrong with OPs code other than being incomplete. Instead of speculating how it could be wrong you could ask for the missing code ;)
– user463035818
1 hour ago




1




1




I would say that the code as is perfectly exemplifies the utility of override. You don't need the base class definition before your eyes. The compiler will tell you if you got it wrong thanks to override.
– StoryTeller
23 mins ago





I would say that the code as is perfectly exemplifies the utility of override. You don't need the base class definition before your eyes. The compiler will tell you if you got it wrong thanks to override.
– StoryTeller
23 mins ago













1 Answer
1






active

oldest

votes

















up vote
13
down vote













You can't put an override specifier when defining the function. The language doesn't allow it, and a compiler will complain. So there's only really one option.



Beyond that, this option also makes more sense. When declaring the function you are expressing an intent to override it. So putting override there at the point of expressing your intent makes sense. That is what you are asking the compiler to check here, your intent to override. The declaration is also enough to verify that function is originally declared virtual and that you got the signature right.






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',
    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%2f53167777%2fwhere-to-use-the-override-keyword-in-c%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    13
    down vote













    You can't put an override specifier when defining the function. The language doesn't allow it, and a compiler will complain. So there's only really one option.



    Beyond that, this option also makes more sense. When declaring the function you are expressing an intent to override it. So putting override there at the point of expressing your intent makes sense. That is what you are asking the compiler to check here, your intent to override. The declaration is also enough to verify that function is originally declared virtual and that you got the signature right.






    share|improve this answer


























      up vote
      13
      down vote













      You can't put an override specifier when defining the function. The language doesn't allow it, and a compiler will complain. So there's only really one option.



      Beyond that, this option also makes more sense. When declaring the function you are expressing an intent to override it. So putting override there at the point of expressing your intent makes sense. That is what you are asking the compiler to check here, your intent to override. The declaration is also enough to verify that function is originally declared virtual and that you got the signature right.






      share|improve this answer
























        up vote
        13
        down vote










        up vote
        13
        down vote









        You can't put an override specifier when defining the function. The language doesn't allow it, and a compiler will complain. So there's only really one option.



        Beyond that, this option also makes more sense. When declaring the function you are expressing an intent to override it. So putting override there at the point of expressing your intent makes sense. That is what you are asking the compiler to check here, your intent to override. The declaration is also enough to verify that function is originally declared virtual and that you got the signature right.






        share|improve this answer














        You can't put an override specifier when defining the function. The language doesn't allow it, and a compiler will complain. So there's only really one option.



        Beyond that, this option also makes more sense. When declaring the function you are expressing an intent to override it. So putting override there at the point of expressing your intent makes sense. That is what you are asking the compiler to check here, your intent to override. The declaration is also enough to verify that function is originally declared virtual and that you got the signature right.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 hours ago

























        answered 2 hours ago









        StoryTeller

        87.6k12174243




        87.6k12174243



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53167777%2fwhere-to-use-the-override-keyword-in-c%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery