Aamir
Aamir

Reputation: 15576

usage of virtual keyword with a class declaration

I was asked in an interview that what is the usage of virtual keyword with a class declaration in C++ and I answered that virtual keyword cannot be used with a class declaration in C++. The interviewer said that it is possible and asked me to test it later.

Now that I have checked it myself I have come to know that this is possible and this is not a compiler error. In fact, when I do something like this with a Visual C++ compiler:

virtual class Test
{
   int i;
};

I get a compiler warning "warning C4091: 'virtual ' : ignored on left of 'Test' when no variable is declared". I haven't been able to find out yet that what this warning means and further what is the usage of virtual keyword. If there is no helpful usage, then why is this allowed in the first place and why is this not a compiler error.

Upvotes: 1

Views: 7641

Answers (6)

GrahamS
GrahamS

Reputation: 10350

Could he have been talking about using a virtual base class in the class declaration?

Like this:

class CashierQueue : virtual public Queue {};

This is used in multiple inheritance when you want to avoid a derived class having multiple copies of the member data when it inherits from two or more classes that share the same base class.

class Queue {};
class CashierQueue : virtual public Queue {};
class LunchQueue : virtual public Queue {};
class LunchCashierQueue : public LunchQueue, public CashierQueue {};

See http://msdn.microsoft.com/en-us/library/wcz57btd(VS.80).aspx

Upvotes: 1

James Hopkin
James Hopkin

Reputation: 13973

That's a bug in VC++. Comeau and gcc both reject the code.

Upvotes: 6

Timo Geusch
Timo Geusch

Reputation: 24351

You're looking at the wrong sort of usage - I'm pretty sure that the interviewer was referring to virtual base class declarations, like so:

  class A : virtual public B, public C {
    ...
  };

This is a common idiom and used to get around the "multiple base class" scenario in diamond-shaped inheritance trees. The typical problem with them is that you inherit from both class B and class C and they share the common ancestor A. If you go 'up' the inheritance tree you'll hit the ambiguity as to which instance of 'A' you're supposed to use. If B & C have A as a virtual base class instead, they will refer to the same instance of A, which solves this problem.

You can find a more exhaustive description with class diagrams here.

Upvotes: 2

Nik
Nik

Reputation: 1364

Maybe he was referring to virtual inheritance / virtual base classes? E.g.

class A : virtual public B
{ ... }

That would technically be part of the class definition.

Upvotes: 1

Sean
Sean

Reputation: 62542

virtual can be used when deriving from another class:

class Foo : public virtual Bar
{
}

This is used to avoid having multiple versions of the same base class when using multiple inheritance. Wikipedia has a good article on the subject.

Upvotes: 6

leiz
leiz

Reputation: 4052

You simply can't use virtual for a class. It only works for member functions

Upvotes: 2

Related Questions