Virtual functions --- Explain me.

V

Victor Bazarov

I would like to know what is virtual function in c++.
Besides what is virtual.

Get a good book and read about them. Virtual functions are
the basis for implementing run-time polymorphism and represent
a huge part of C++ language. It is impossible to cover them
in a newsgroup post.
 
E

E. Robert Tisdale

I would like to know what is virtual function in C++.
Besides what is virtual.

A virtual function is not necessarily
the actual function called when it is invoked:
cat main.cpp
#include <iostream>

class Base {
public:
void f(void) const {
std::cerr << "Base::f(void)" << std::endl;
}
virtual
void g(void) const {
std::cerr << "Base::g(void)" << std::endl;
}
};

void test(const Base& b) {
b.f();
b.g();
}

class Derived: public Base {
public:
void f(void) const {
std::cerr << "Derived::f(void)" << std::endl;
}
virtual
void g(void) const {
std::cerr << "Derived::g(void)" << std::endl;
}
};

int main(int argc, char* argv[]) {
Base b;
test(b);
Derived d;
test(d);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cpp
./main
Base::f(void)
Base::g(void)
Base::f(void)
Derived::g(void)

The function test(const Base&) is passed a [const] *reference*
to an object of the Base type
so it must always call function f(void) for the Base type
but the actual function called by virtual function g(void)
depends upon the actual type of the object reference through b.
 
G

Gregory L. Hansen

I would like to know what is virtual function in c++.
Besides what is virtual.

A virtual member function in a base class is overridden by a member
function of the same name in a derived class.
 
H

Howard

Gregory L. Hansen said:
A virtual member function in a base class is overridden by a member
function of the same name in a derived class.

There's a bit more to the subject than that, isn't there? :)

And, it doesn't *have* to be overridden...ever. You can make a function
virtual just *in case* you want to override it later.

-Howard
 
A

Andrew McDonagh

Victor said:
There are some small yet very important differences.

eh?

What differences?

Aside from all methods are virtual in Java by default.
Java methods can be made non-virtual by appending the 'final' keyword to
them as in:


public final void someMethod() {
}
 
V

Victor Bazarov

Andrew said:
eh?

What differences?

Methods in Java called virtually from constructors. In C++ inside
a constructor the virtual function is that of the object under
construction.
Aside from all methods are virtual in Java by default.
Java methods can be made non-virtual by appending the 'final' keyword to
them as in:


public final void someMethod() {
}

They are not "made non-virtual" -- wrong choice of words.

V
 
M

Matthias Kaeppler

Howard said:
There's a bit more to the subject than that, isn't there? :)

And, it doesn't *have* to be overridden...ever. You can make a function
virtual just *in case* you want to override it later.

-Howard

Also, the overriding function must not only have the same name, but the
same signature.
 

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

Similar Threads

Virtual destructor 3
Wrapper functions 1
Non virtual and inheritance 7
Functions 2
About me... 0
Delegation through pure virtual 15
Pure virtual accessors? 5
Please explain: #define MOVE 0x05 1

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top