Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I've seen some strange code below.
C++
class AA
{};
class BB: virtual public AA
{};

What is the "virtual public" means? And there it can be changed into "virtual private" and "virtual protected"?

If so, what is the difference between them?

Anyone notice me.
Posted
Comments
PIEBALDconsult 4-Jul-14 0:08am    
Virtual and public are two different things.
WuRunZhe 4-Jul-14 2:17am    
Yes, you're right. But how can you explain of that? "Virtual" keyword is used at inheritance.
Sergey Alexandrovich Kryukov 4-Jul-14 1:06am    
There is no such thing. I doubt someone would seriously volunteer to explain you those two concepts. It looks like you are trying to code by trial-and-error method, not understanding what you are writing. It never works. You need to read a book on OOP to understand "virtual". It can be explained in one article, but only to the people who has serious background in many other aspects of programming. And such people who are literate but never understood how OOP works usually become highly surprised/excited. So, grab a book and start learn something serious.

Sorry for not answering the question. I have serious reasons not to do it. And I think my advice above will be much better for you.

—SA
WuRunZhe 4-Jul-14 2:16am    
You mean, "virtual" keyword could not be used like that?
Sergey Alexandrovich Kryukov 4-Jul-14 2:28am    
Sorry, I did not realize what is your concern; but you also wasn't clear and correct in this question. Now, I can answer your question.
—SA

1 solution

This specific use of virtual is related to quite a delicate feature of C++, specific to C++, called virtual base classes. You can understand how it works from this chapter of this manual:
http://www.learncpp.com/cpp-tutorial/118-virtual-base-classes[^].

This is some kind of complexity related to C++ multiple inheritance. When you have the same inherited members in more then one inheritance path on the inheritance graph, their meaning could be ambiguous. C++ offers two ways to derive from a base class, two options for inheritance.

—SA
 
Share this answer
 
v3
Comments
WuRunZhe 4-Jul-14 2:35am    
Thank you for your answer. I've understood about it very clearly.
Sergey Alexandrovich Kryukov 4-Jul-14 3:01am    
That's great. My pleasure.
—SA
CPallini 4-Jul-14 2:49am    
5.
Sergey Alexandrovich Kryukov 4-Jul-14 3:01am    
Thank you, Carlo.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900