Different functions?












0















So here is code sample. The task was to give output that this code will print out. Is it 2 different functions? What happens with vtable in B class then?
Does it just store 2 pointers on 2 different functions with same name?



#include<iostream>
#include <vector>
using namespace std;
class A
{
public:
A()
{
init();
}
virtual void init(bool a = true)
{
if(a)
cout << "A" << endl;
}
};
class B :public A
{
public:
virtual void init()
{
cout << "B" << endl;
}
};

int main()
{
B b;
A* a = &b;
a->init();
a->init(true);
system("pause");
}


Couldn't really find where to read about this case. Could you mates explain or give a link to some source if you've seen this case?










share|improve this question

























  • Try compiling it with warnings enabled - you should get at least one helpful warning which will give you some insight...

    – Paul R
    Nov 13 '18 at 16:05








  • 1





    Oh, clang is evidently more helpful then... <stdin>:21:18: warning: 'B::init' hides overloaded virtual function [-Woverloaded-virtual] virtual void init() ^ <stdin>:12:18: note: hidden overloaded virtual function 'A::init' declared here: different number of parameters (1 vs 0) virtual void init(bool a = true)

    – Paul R
    Nov 13 '18 at 16:07








  • 1





    This is the reason for override.

    – molbdnilo
    Nov 13 '18 at 16:09






  • 1





    Every function you write is different from others. The real question is «when I make a call, which one is selected?»

    – Jean-Baptiste Yunès
    Nov 13 '18 at 16:22






  • 2





    @PaulR gcc will warn if you explicitly add the -Woverloaded-virtual switch which you have shown in the clang warning message.

    – Pates
    Nov 13 '18 at 20:00
















0















So here is code sample. The task was to give output that this code will print out. Is it 2 different functions? What happens with vtable in B class then?
Does it just store 2 pointers on 2 different functions with same name?



#include<iostream>
#include <vector>
using namespace std;
class A
{
public:
A()
{
init();
}
virtual void init(bool a = true)
{
if(a)
cout << "A" << endl;
}
};
class B :public A
{
public:
virtual void init()
{
cout << "B" << endl;
}
};

int main()
{
B b;
A* a = &b;
a->init();
a->init(true);
system("pause");
}


Couldn't really find where to read about this case. Could you mates explain or give a link to some source if you've seen this case?










share|improve this question

























  • Try compiling it with warnings enabled - you should get at least one helpful warning which will give you some insight...

    – Paul R
    Nov 13 '18 at 16:05








  • 1





    Oh, clang is evidently more helpful then... <stdin>:21:18: warning: 'B::init' hides overloaded virtual function [-Woverloaded-virtual] virtual void init() ^ <stdin>:12:18: note: hidden overloaded virtual function 'A::init' declared here: different number of parameters (1 vs 0) virtual void init(bool a = true)

    – Paul R
    Nov 13 '18 at 16:07








  • 1





    This is the reason for override.

    – molbdnilo
    Nov 13 '18 at 16:09






  • 1





    Every function you write is different from others. The real question is «when I make a call, which one is selected?»

    – Jean-Baptiste Yunès
    Nov 13 '18 at 16:22






  • 2





    @PaulR gcc will warn if you explicitly add the -Woverloaded-virtual switch which you have shown in the clang warning message.

    – Pates
    Nov 13 '18 at 20:00














0












0








0








So here is code sample. The task was to give output that this code will print out. Is it 2 different functions? What happens with vtable in B class then?
Does it just store 2 pointers on 2 different functions with same name?



#include<iostream>
#include <vector>
using namespace std;
class A
{
public:
A()
{
init();
}
virtual void init(bool a = true)
{
if(a)
cout << "A" << endl;
}
};
class B :public A
{
public:
virtual void init()
{
cout << "B" << endl;
}
};

int main()
{
B b;
A* a = &b;
a->init();
a->init(true);
system("pause");
}


Couldn't really find where to read about this case. Could you mates explain or give a link to some source if you've seen this case?










share|improve this question
















So here is code sample. The task was to give output that this code will print out. Is it 2 different functions? What happens with vtable in B class then?
Does it just store 2 pointers on 2 different functions with same name?



#include<iostream>
#include <vector>
using namespace std;
class A
{
public:
A()
{
init();
}
virtual void init(bool a = true)
{
if(a)
cout << "A" << endl;
}
};
class B :public A
{
public:
virtual void init()
{
cout << "B" << endl;
}
};

int main()
{
B b;
A* a = &b;
a->init();
a->init(true);
system("pause");
}


Couldn't really find where to read about this case. Could you mates explain or give a link to some source if you've seen this case?







c++ override signature virtual-functions default-arguments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:01









curiousguy

4,54523044




4,54523044










asked Nov 13 '18 at 16:02









Edwin PacoEdwin Paco

95




95













  • Try compiling it with warnings enabled - you should get at least one helpful warning which will give you some insight...

    – Paul R
    Nov 13 '18 at 16:05








  • 1





    Oh, clang is evidently more helpful then... <stdin>:21:18: warning: 'B::init' hides overloaded virtual function [-Woverloaded-virtual] virtual void init() ^ <stdin>:12:18: note: hidden overloaded virtual function 'A::init' declared here: different number of parameters (1 vs 0) virtual void init(bool a = true)

    – Paul R
    Nov 13 '18 at 16:07








  • 1





    This is the reason for override.

    – molbdnilo
    Nov 13 '18 at 16:09






  • 1





    Every function you write is different from others. The real question is «when I make a call, which one is selected?»

    – Jean-Baptiste Yunès
    Nov 13 '18 at 16:22






  • 2





    @PaulR gcc will warn if you explicitly add the -Woverloaded-virtual switch which you have shown in the clang warning message.

    – Pates
    Nov 13 '18 at 20:00



















  • Try compiling it with warnings enabled - you should get at least one helpful warning which will give you some insight...

    – Paul R
    Nov 13 '18 at 16:05








  • 1





    Oh, clang is evidently more helpful then... <stdin>:21:18: warning: 'B::init' hides overloaded virtual function [-Woverloaded-virtual] virtual void init() ^ <stdin>:12:18: note: hidden overloaded virtual function 'A::init' declared here: different number of parameters (1 vs 0) virtual void init(bool a = true)

    – Paul R
    Nov 13 '18 at 16:07








  • 1





    This is the reason for override.

    – molbdnilo
    Nov 13 '18 at 16:09






  • 1





    Every function you write is different from others. The real question is «when I make a call, which one is selected?»

    – Jean-Baptiste Yunès
    Nov 13 '18 at 16:22






  • 2





    @PaulR gcc will warn if you explicitly add the -Woverloaded-virtual switch which you have shown in the clang warning message.

    – Pates
    Nov 13 '18 at 20:00

















Try compiling it with warnings enabled - you should get at least one helpful warning which will give you some insight...

– Paul R
Nov 13 '18 at 16:05







Try compiling it with warnings enabled - you should get at least one helpful warning which will give you some insight...

– Paul R
Nov 13 '18 at 16:05






1




1





Oh, clang is evidently more helpful then... <stdin>:21:18: warning: 'B::init' hides overloaded virtual function [-Woverloaded-virtual] virtual void init() ^ <stdin>:12:18: note: hidden overloaded virtual function 'A::init' declared here: different number of parameters (1 vs 0) virtual void init(bool a = true)

– Paul R
Nov 13 '18 at 16:07







Oh, clang is evidently more helpful then... <stdin>:21:18: warning: 'B::init' hides overloaded virtual function [-Woverloaded-virtual] virtual void init() ^ <stdin>:12:18: note: hidden overloaded virtual function 'A::init' declared here: different number of parameters (1 vs 0) virtual void init(bool a = true)

– Paul R
Nov 13 '18 at 16:07






1




1





This is the reason for override.

– molbdnilo
Nov 13 '18 at 16:09





This is the reason for override.

– molbdnilo
Nov 13 '18 at 16:09




1




1





Every function you write is different from others. The real question is «when I make a call, which one is selected?»

– Jean-Baptiste Yunès
Nov 13 '18 at 16:22





Every function you write is different from others. The real question is «when I make a call, which one is selected?»

– Jean-Baptiste Yunès
Nov 13 '18 at 16:22




2




2





@PaulR gcc will warn if you explicitly add the -Woverloaded-virtual switch which you have shown in the clang warning message.

– Pates
Nov 13 '18 at 20:00





@PaulR gcc will warn if you explicitly add the -Woverloaded-virtual switch which you have shown in the clang warning message.

– Pates
Nov 13 '18 at 20:00












1 Answer
1






active

oldest

votes


















2














They were already two different functions (overriding doesn't change that), but because they have a different signature, the one in B does not override the one in A.



Remember, the name of a function is only part of its identity! Its parameter list matters too.



If you'd put the override keyword on B::init() then your program would have failed to compile because B::init() doesn't actually override anything (there is no init(), virtual or otherwise, in its base).



Nothing really "happens" with the vtable that wouldn't also have happened if the two functions literally had different names, like A::init(bool) and B::urgleburgleboop().



Note that, quite aside from virtual and polymorphism and overriding, B::init() also "hides" A::init(bool) for normal overload resolution (thanks, C++!), and because of this Clang will warn on your code.



As for where you can read about it, your C++ book would be a good start. :)






share|improve this answer
























  • Good answer, I was writing one using nm and showing that the mangled symbols are different for the two methods signatures but the theory is enough there.

    – Tezirg
    Nov 13 '18 at 16:14











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%2f53284920%2fdifferent-functions%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









2














They were already two different functions (overriding doesn't change that), but because they have a different signature, the one in B does not override the one in A.



Remember, the name of a function is only part of its identity! Its parameter list matters too.



If you'd put the override keyword on B::init() then your program would have failed to compile because B::init() doesn't actually override anything (there is no init(), virtual or otherwise, in its base).



Nothing really "happens" with the vtable that wouldn't also have happened if the two functions literally had different names, like A::init(bool) and B::urgleburgleboop().



Note that, quite aside from virtual and polymorphism and overriding, B::init() also "hides" A::init(bool) for normal overload resolution (thanks, C++!), and because of this Clang will warn on your code.



As for where you can read about it, your C++ book would be a good start. :)






share|improve this answer
























  • Good answer, I was writing one using nm and showing that the mangled symbols are different for the two methods signatures but the theory is enough there.

    – Tezirg
    Nov 13 '18 at 16:14
















2














They were already two different functions (overriding doesn't change that), but because they have a different signature, the one in B does not override the one in A.



Remember, the name of a function is only part of its identity! Its parameter list matters too.



If you'd put the override keyword on B::init() then your program would have failed to compile because B::init() doesn't actually override anything (there is no init(), virtual or otherwise, in its base).



Nothing really "happens" with the vtable that wouldn't also have happened if the two functions literally had different names, like A::init(bool) and B::urgleburgleboop().



Note that, quite aside from virtual and polymorphism and overriding, B::init() also "hides" A::init(bool) for normal overload resolution (thanks, C++!), and because of this Clang will warn on your code.



As for where you can read about it, your C++ book would be a good start. :)






share|improve this answer
























  • Good answer, I was writing one using nm and showing that the mangled symbols are different for the two methods signatures but the theory is enough there.

    – Tezirg
    Nov 13 '18 at 16:14














2












2








2







They were already two different functions (overriding doesn't change that), but because they have a different signature, the one in B does not override the one in A.



Remember, the name of a function is only part of its identity! Its parameter list matters too.



If you'd put the override keyword on B::init() then your program would have failed to compile because B::init() doesn't actually override anything (there is no init(), virtual or otherwise, in its base).



Nothing really "happens" with the vtable that wouldn't also have happened if the two functions literally had different names, like A::init(bool) and B::urgleburgleboop().



Note that, quite aside from virtual and polymorphism and overriding, B::init() also "hides" A::init(bool) for normal overload resolution (thanks, C++!), and because of this Clang will warn on your code.



As for where you can read about it, your C++ book would be a good start. :)






share|improve this answer













They were already two different functions (overriding doesn't change that), but because they have a different signature, the one in B does not override the one in A.



Remember, the name of a function is only part of its identity! Its parameter list matters too.



If you'd put the override keyword on B::init() then your program would have failed to compile because B::init() doesn't actually override anything (there is no init(), virtual or otherwise, in its base).



Nothing really "happens" with the vtable that wouldn't also have happened if the two functions literally had different names, like A::init(bool) and B::urgleburgleboop().



Note that, quite aside from virtual and polymorphism and overriding, B::init() also "hides" A::init(bool) for normal overload resolution (thanks, C++!), and because of this Clang will warn on your code.



As for where you can read about it, your C++ book would be a good start. :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 16:09









Lightness Races in OrbitLightness Races in Orbit

288k51470798




288k51470798













  • Good answer, I was writing one using nm and showing that the mangled symbols are different for the two methods signatures but the theory is enough there.

    – Tezirg
    Nov 13 '18 at 16:14



















  • Good answer, I was writing one using nm and showing that the mangled symbols are different for the two methods signatures but the theory is enough there.

    – Tezirg
    Nov 13 '18 at 16:14

















Good answer, I was writing one using nm and showing that the mangled symbols are different for the two methods signatures but the theory is enough there.

– Tezirg
Nov 13 '18 at 16:14





Good answer, I was writing one using nm and showing that the mangled symbols are different for the two methods signatures but the theory is enough there.

– Tezirg
Nov 13 '18 at 16:14


















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%2f53284920%2fdifferent-functions%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

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation