answersLogoWhite

0

When do you make a virtual?

User Avatar

Anonymous

∙ 14y ago
Updated: 4/18/2023

Whenever a derived class requires direct inheritance from a base class, even if it inherits that base class indirectly. That is, if V is a base class from which B is derived, and D is derived from B, then D inherits from V indirectly (through B). But if B is virtually derived from V, then D will inherit directly from V.

This feature is commonly used in conjunction with multiple inheritance. Examine the following declarations:

class V{};

class B1: public V{};

class B2: public V{};

class M: public B1, public B2{};

Now suppose you have the following code:

M m; // Declare an instance of M.

V& v = m; // Ambiguous...

The problem with this is that M inherits V from both B1 and B2, and therefore inherits two separate instances of V. The compiler is unable to determine which instance of V you want to refer to. One solution to this would be to use static casts to indirectly refer to an explicit instance of V:

V& v = static_cast<B1&>(m);

or

V& v = static_cast<B2&>(m);

While this is certainly workable, it is an ugly approach that places far too much responsibility upon the programmer to ensure the correct instance of V is being referred to. However, unless there is a specific need to have two instances of V within M, the problem can be resolved with virtual inheritance.

By virtually deriving both B1 and B2 from V, M will directly inherit just one instance of V, which is then shared, virtually, between B1 and B2:

class V{};

class B1: public virtual V{};

class B2: public virtual V{};

class M: public B1, public B2{};

M m;

V& v = m; // No ambiguity.

Now M can access all the members of V directly, as can B1 and B2, because they now share the same instance of V.

Note that it doesn't matter whether the virtual keyword is placed before or after the access specifier (which is public in this case). "virtual public" and "public virtual" have the same meaning.

User Avatar

Geovanny Nader

Lvl 10
∙ 3y ago

What else can I help you with?

Related Questions

How Can you make a virtual world?

you go to google type in how you can make a virtual world


But they made a virtual villagers 4 when will they make virtual vlillagers 5?

virtual villagers 5 has already been made. it is called virtual villagers new believers.


Does virtual boy make you blind?

No i does not.


How can you make a VPN with a Virtual server with only 1 IP address?

in virtual we select the custom specific virtual network and other in bridgenetwork


Are their virtual worlds were you can get a job and raise a family?

Well, virtual families and virtual families 2 are good. They are by the game producers Last Day of Work, who also make virtual villagers.


How can use of virtual reality?

It can make you addicted


Where can you make your own virtual clothes?

Barbie.com


What do you do to make your own virtual world website?

go onto youtube and type in (') how to make a virtual world website (') without the comas and brackets. then it may tell you


What are the signs of virtual cheating?

This will be a bit complex, but virtual cheating is all about make believe. Like a cartoon on TV, or avatar. There's no sign that you can have or wonder but in reality virtual is all about fantasy and make believe. It's a character in the virtual games. Man or woman who are addicted on computers do make their fantasy real by creating a virtual person. This are part of a problem when couples are concern, just like being on dating site but different because they are controlling what they have.


How do you make good virtual pets on gaia?

You don't....


How do you make a virtual world?

Using VastPark Creator.


How do you make a virtual dragon zoo?

HOW should we know.