Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help regarding virtual function.

Reply
Thread Tools

Help regarding virtual function.

 
 
trialproduct2004@yahoo.com
Guest
Posts: n/a
 
      04-26-2006
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-26-2006
* http://www.velocityreviews.com/forums/(E-Mail 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      04-27-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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?


 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      04-27-2006

> 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.


The following article should help:
http://www.eventhelix.com/RealtimeMa...rformance2.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Interaction Modeling
Tool

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question regarding to the keyword "virtual" wizwx C++ 4 03-23-2007 02:18 PM
a simple question regarding virtual directories Developer.Man4@gmail.com ASP .Net 1 02-06-2007 07:52 PM
Question regarding virtual functions/destructors Pravesh C++ 3 06-20-2006 08:36 AM
Regarding Virtual Constructor ramesh C++ 4 05-22-2006 08:07 PM
question regarding overriding of web.config in the root directory..in a web app in a virtual directory dotnetprogram ASP .Net 1 12-27-2003 06:02 AM



Advertisments