Applying “using” keyword on C++ pure virtual function
The Class B is overriding the pure Virtual Function "print()" of class A. Class C is inheriting Class B as well as having a "using A::print" statement.
Now why Class C is not an abstract class?
class A {
public :
virtual void print() =0;
};
class B:public A {
public:
void print();
};
void B :: print() {
cout << "nClass B print ()";
}
class C : public B {
public:
using A::print;
};
void funca (A *a) {
// a->print(1);
}
void funcb (B *b) {
b->print();
}
void funcc (C *c) {
c->print();
}
int main() {
B b;
C c;
funca(&c);
funcb(&c);
funcc(&c);
return 0;
}
Output:
Class B print ()
Class B print ()
c++ using virtual-functions
add a comment |
The Class B is overriding the pure Virtual Function "print()" of class A. Class C is inheriting Class B as well as having a "using A::print" statement.
Now why Class C is not an abstract class?
class A {
public :
virtual void print() =0;
};
class B:public A {
public:
void print();
};
void B :: print() {
cout << "nClass B print ()";
}
class C : public B {
public:
using A::print;
};
void funca (A *a) {
// a->print(1);
}
void funcb (B *b) {
b->print();
}
void funcc (C *c) {
c->print();
}
int main() {
B b;
C c;
funca(&c);
funcb(&c);
funcc(&c);
return 0;
}
Output:
Class B print ()
Class B print ()
c++ using virtual-functions
Kinda related, since a using declaration is not an overrider.
– StoryTeller
1 hour ago
add a comment |
The Class B is overriding the pure Virtual Function "print()" of class A. Class C is inheriting Class B as well as having a "using A::print" statement.
Now why Class C is not an abstract class?
class A {
public :
virtual void print() =0;
};
class B:public A {
public:
void print();
};
void B :: print() {
cout << "nClass B print ()";
}
class C : public B {
public:
using A::print;
};
void funca (A *a) {
// a->print(1);
}
void funcb (B *b) {
b->print();
}
void funcc (C *c) {
c->print();
}
int main() {
B b;
C c;
funca(&c);
funcb(&c);
funcc(&c);
return 0;
}
Output:
Class B print ()
Class B print ()
c++ using virtual-functions
The Class B is overriding the pure Virtual Function "print()" of class A. Class C is inheriting Class B as well as having a "using A::print" statement.
Now why Class C is not an abstract class?
class A {
public :
virtual void print() =0;
};
class B:public A {
public:
void print();
};
void B :: print() {
cout << "nClass B print ()";
}
class C : public B {
public:
using A::print;
};
void funca (A *a) {
// a->print(1);
}
void funcb (B *b) {
b->print();
}
void funcc (C *c) {
c->print();
}
int main() {
B b;
C c;
funca(&c);
funcb(&c);
funcc(&c);
return 0;
}
Output:
Class B print ()
Class B print ()
c++ using virtual-functions
c++ using virtual-functions
asked 1 hour ago
Gtrex
411
411
Kinda related, since a using declaration is not an overrider.
– StoryTeller
1 hour ago
add a comment |
Kinda related, since a using declaration is not an overrider.
– StoryTeller
1 hour ago
Kinda related, since a using declaration is not an overrider.
– StoryTeller
1 hour ago
Kinda related, since a using declaration is not an overrider.
– StoryTeller
1 hour ago
add a comment |
1 Answer
1
active
oldest
votes
From cppreference we read that
Using-declaration introduces a member of a base class into the derived class definition [...] If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
As C
inherits from B
, there is already a virtual print
member function, which is non-pure. This one hides/overrides the member function brought in by using A::print
, and hence C
is not an abstract class.
That is just wrong,derived class already has a member
, means a member first declared in this class. See demo here: godbolt.org/z/ff5cEb
– Oliv
56 mins ago
@Oliv Not sure if I get your point, why does it behave differently then based on the virtualness of the member function in question? If hiding or overriding both leads to an exclusion of the set of declarations introduced by the using declaration, shouldn't it exhibit identical behavior?
– lubgr
31 mins ago
Your right this does not apply too to this case. Neither is the standard paragraph you site...
– Oliv
5 mins ago
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%2f54038898%2fapplying-using-keyword-on-c-pure-virtual-function%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
From cppreference we read that
Using-declaration introduces a member of a base class into the derived class definition [...] If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
As C
inherits from B
, there is already a virtual print
member function, which is non-pure. This one hides/overrides the member function brought in by using A::print
, and hence C
is not an abstract class.
That is just wrong,derived class already has a member
, means a member first declared in this class. See demo here: godbolt.org/z/ff5cEb
– Oliv
56 mins ago
@Oliv Not sure if I get your point, why does it behave differently then based on the virtualness of the member function in question? If hiding or overriding both leads to an exclusion of the set of declarations introduced by the using declaration, shouldn't it exhibit identical behavior?
– lubgr
31 mins ago
Your right this does not apply too to this case. Neither is the standard paragraph you site...
– Oliv
5 mins ago
add a comment |
From cppreference we read that
Using-declaration introduces a member of a base class into the derived class definition [...] If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
As C
inherits from B
, there is already a virtual print
member function, which is non-pure. This one hides/overrides the member function brought in by using A::print
, and hence C
is not an abstract class.
That is just wrong,derived class already has a member
, means a member first declared in this class. See demo here: godbolt.org/z/ff5cEb
– Oliv
56 mins ago
@Oliv Not sure if I get your point, why does it behave differently then based on the virtualness of the member function in question? If hiding or overriding both leads to an exclusion of the set of declarations introduced by the using declaration, shouldn't it exhibit identical behavior?
– lubgr
31 mins ago
Your right this does not apply too to this case. Neither is the standard paragraph you site...
– Oliv
5 mins ago
add a comment |
From cppreference we read that
Using-declaration introduces a member of a base class into the derived class definition [...] If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
As C
inherits from B
, there is already a virtual print
member function, which is non-pure. This one hides/overrides the member function brought in by using A::print
, and hence C
is not an abstract class.
From cppreference we read that
Using-declaration introduces a member of a base class into the derived class definition [...] If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
As C
inherits from B
, there is already a virtual print
member function, which is non-pure. This one hides/overrides the member function brought in by using A::print
, and hence C
is not an abstract class.
answered 1 hour ago
lubgr
10.3k21745
10.3k21745
That is just wrong,derived class already has a member
, means a member first declared in this class. See demo here: godbolt.org/z/ff5cEb
– Oliv
56 mins ago
@Oliv Not sure if I get your point, why does it behave differently then based on the virtualness of the member function in question? If hiding or overriding both leads to an exclusion of the set of declarations introduced by the using declaration, shouldn't it exhibit identical behavior?
– lubgr
31 mins ago
Your right this does not apply too to this case. Neither is the standard paragraph you site...
– Oliv
5 mins ago
add a comment |
That is just wrong,derived class already has a member
, means a member first declared in this class. See demo here: godbolt.org/z/ff5cEb
– Oliv
56 mins ago
@Oliv Not sure if I get your point, why does it behave differently then based on the virtualness of the member function in question? If hiding or overriding both leads to an exclusion of the set of declarations introduced by the using declaration, shouldn't it exhibit identical behavior?
– lubgr
31 mins ago
Your right this does not apply too to this case. Neither is the standard paragraph you site...
– Oliv
5 mins ago
That is just wrong,
derived class already has a member
, means a member first declared in this class. See demo here: godbolt.org/z/ff5cEb– Oliv
56 mins ago
That is just wrong,
derived class already has a member
, means a member first declared in this class. See demo here: godbolt.org/z/ff5cEb– Oliv
56 mins ago
@Oliv Not sure if I get your point, why does it behave differently then based on the virtualness of the member function in question? If hiding or overriding both leads to an exclusion of the set of declarations introduced by the using declaration, shouldn't it exhibit identical behavior?
– lubgr
31 mins ago
@Oliv Not sure if I get your point, why does it behave differently then based on the virtualness of the member function in question? If hiding or overriding both leads to an exclusion of the set of declarations introduced by the using declaration, shouldn't it exhibit identical behavior?
– lubgr
31 mins ago
Your right this does not apply too to this case. Neither is the standard paragraph you site...
– Oliv
5 mins ago
Your right this does not apply too to this case. Neither is the standard paragraph you site...
– Oliv
5 mins ago
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f54038898%2fapplying-using-keyword-on-c-pure-virtual-function%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
Kinda related, since a using declaration is not an overrider.
– StoryTeller
1 hour ago