Where to use the override keyword in C++
Clash 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;
c++ c++11
 |Â
show 1 more comment
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;
c++ c++11
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? PresumablyInfinityStone
defines it as virtual.
– StoryTeller
2 hours ago
@StoryTeller just an assumption, class isn'tfinal
, inheritance ispublic
and the overridden method isprivate
. 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 ofoverride
. 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
 |Â
show 1 more comment
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;
c++ c++11
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
c++ c++11
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? PresumablyInfinityStone
defines it as virtual.
– StoryTeller
2 hours ago
@StoryTeller just an assumption, class isn'tfinal
, inheritance ispublic
and the overridden method isprivate
. 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 ofoverride
. 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
 |Â
show 1 more comment
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? PresumablyInfinityStone
defines it as virtual.
– StoryTeller
2 hours ago
@StoryTeller just an assumption, class isn'tfinal
, inheritance ispublic
and the overridden method isprivate
. 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 ofoverride
. 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
 |Â
show 1 more comment
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 2 hours ago
answered 2 hours ago
StoryTeller
87.6k12174243
87.6k12174243
add a comment |Â
add a comment |Â
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
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
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
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
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
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 ispublic
and the overridden method isprivate
. 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