Help regarding virtual function.

T

trialproduct2004

Hi all,

Can someone tell me how virtual functions works.

Like how memory is getting allocated to virtual function. And how to
call base class function through derived class pointer.
And why virtual function can be access only through pointer.

Thanks in advance.
 
A

Alf P. Steinbach

* (e-mail address removed):
Can someone tell me how virtual functions works.

The nearest C++ textbook.

You can try

#include <iostream>
#include <ostream>

void say( char const s[] ) { std::cout << s << std::endl; }

struct Base
{
virtual void foo() const { say( "Base::foo" ); }
};

struct Derived: Base
{
virtual void foo() const { say( "Derived::foo" ); }
};

int main()
{
Base const& baseRef = Derived();
baseRef.foo();
}

Like how memory is getting allocated to virtual function.

It isn't.

And how to call base class function through derived class pointer.
p->Base::foo();


And why virtual function can be access only through pointer.

There's no such restriction.
 
J

Jim Langston

Hi all,

Can someone tell me how virtual functions works.

Like how memory is getting allocated to virtual function.

It's implementation dependant, but usually (if not always) you only have one
copy of the function in the executable. That's why methods usually don't
add to an objects size.

On my system this program outputs
4 4

#include <iostream>
#include <string>

class Class1
{
int i;
};

class Class2
{
int i;
void Output() { std::cout << i; };
};

int main ()
{

std::cout << sizeof(Class1) << " " << sizeof(Class2);

std::string wait;
std::cin >> wait;
}

Methods do not usually add to a class's size for storage. They are just an
executable function in the executable usually called through some lookup
table.
And how to
call base class function through derived class pointer.

Study the output of this program.

#include <iostream>
#include <string>

class Base
{
public:
virtual void Output() { std::cout << "Base" << std::endl; };
};

class Derived: public Base
{
public:
void Output() { std::cout << "Derived" << std::endl; Base::Output(); };
};

int main ()
{
Derived Instance;
Base* BaseP = &Instance;

std::cout << "Derived call of Output()" << std::endl;
Instance.Output();
std::cout << "Base Pointer call of Output()" << std::endl;
BaseP->Output();
std::cout << "Base Pointer call of Base::Output()" << std::endl;
BaseP->Base::Output();

std::string wait;
std::cin >> wait;
}

Output:
Derived call of Output()
Derived
Base
Base Pointer call of Output()
Derived
Base
Base Pointer call of Base::Output()
Base

And why virtual function can be access only through pointer.

Look at the above program. As you see, Instance which is an instance of the
derived class has it's virtual function accesed and it's not a pointer.
What do you mean?
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top