tags:

views:

672

answers:

6

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.

A: 

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

leiz
It works with Microsoft Visual C++ Compiler and generates only a warning and not an error as I have already said.
Aamir
you can "virtual inheritance", but i have never seen virtual keyword before class
leiz
@Aamir: The VC compiler, and to a lesser extend g++, are poor choices for compilers to use to verify your code. If you want to test for standard code, check out the Comeau try it online: http://www.comeaucomputing.com/tryitout/
Richard Corden
+2  A: 

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.

Sean
I definitely know about using virtual in this way. But please look at my example that I have given in the original question. I just want to ask that why is this allowed if there is no usage for it.
Aamir
+1  A: 

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.

Nik
+1  A: 

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.

Timo Geusch
As I have already replied in another comment that I know about this sort of usage very well and I had good discussion with the interviewer about the sort of usage that you are mentioning. But he came up with this later (the thing that I have asked). It is quite possible that he didn't know this himself. :)
Aamir
+5  A: 

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

James Hopkin
I didn't know that this doesn't work with other compilers. You are probably the only person who seems to have understood my question.
Aamir
Always worth trying it on more that one compiler. Gcc is free, and you can try code out for free on Comeau here: http://www.comeaucomputing.com/tryitout
James Hopkin
Aamir, you seem to have two questions: (1) what did the interviewer mean? (2) Why is this legal syntax? I think most people have tried to answer (1) rather than (2).
GrahamS
Yes, I think you are right. Probably I wasn't clear enough while asking this question. Anyway, I have stopped replying to the answers related to (1) now.
Aamir
+1  A: 

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

GrahamS