virtual

  • Thread starter richard pickworth
  • Start date
A

Alf P. Steinbach

* richard pickworth:
What does "virtual" mean?

Consider

struct A{ virtual void foo(){} };
struct B: A {}
struct C: B { virtual void foo(){} };

and

int main()
{
A* a = new A; A* b = new B; A* c = new C;

a->foo();
b->foo();
c->foo();

delete c; delete b; delete a;
}

For the a->foo() call A::foo is executed.

For the b->foo() call A::foo is executed. Since B doesn't provide
any foo() implementation there isn't any other possibility. 'virtual'
or not.

For the c->foo() call the result depends on whether foo() is virtual
or not. The statically known type for c is A*, a pointer to an A-object.
So if foo() wasn't virtual A::foo() would have been executed, according
to the type of object known at this place in the program text at compile
time.

But foo() is 'virtual' in A, and therefore also in all derived
classes, so the c->foo() results in a check of the _dynamic_ (run-time)
type of the object pointed to by c, which here is C, and the function
executed is _as if_ a search for foo() started in class C and then went up
the base class chain until found; here foo() is found in class C.

A typical C++ compiler will emit equivalent machine code that completely
avoid the search up the base class chain, by using table look-ups.
 
U

Uenal Mutlu

richard pickworth said:
What does "virtual" mean?

Does your C++ book not explain it?
Do some research yourself: use a search engine (like www.google.com) to
find the answer to such basic questions.
There's also a newsgroup for C/C++ newbies: alt.comp.lang.learn.c-c++
 
P

Peter Julian

richard pickworth said:
What does "virtual" mean?
thanks
richard

Virtual means dynamic binding. In other words, an object's behaviour is not
determined until run-time. The mechanism that provides this in C++ is the
vtable which is created at compile time but populated at run-time.

Way back when, if you had a bunch of related classes and needed to store
them into a container, you needed a different container for each related
type. With virtual member functions, you can now create a container based on
an abstract base class (called a pure-virtual class).

A shape, an animal, a vehicle, are good examples of abstract classes. You
can't create a shape or animal (they are abstract), but you can derive from
these to create a circle, a cat or a truck. This is important because now
you can store derived classes in a container holding an abstract type. One
container can now store any derivative of the stored base-class and each
element's virtual behaviours are determined at runtime.

In effect, virtual in C++ allows each element to determine its behaviour at
run-time. No longer does the container determine the behaviour at compile
time.
 
K

Karl Heinz Buchegger

richard said:
I visited the mewbies group, they sent me here.
looks like I'll have to go back.

Looks like you need some books :)
Seriously. C++ is much to complex that you can get
away without (at least) one book.
 
R

richard pickworth

I visited the mewbies group, they sent me here.
looks like I'll have to go back.
yours
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top