473,662 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help regarding virtual function.

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.

Apr 26 '06 #1
3 1727
* tr************* *@yahoo.com:

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::f oo" ); }
};

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?
Apr 26 '06 #2

<tr************ **@yahoo.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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?
Apr 27 '06 #3
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

Apr 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2113
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I can get! Consider 3 classes in the following heirarchy: base / \ deriv1 deriv2 \
2
1486
by: He Shiming | last post by:
Hi, I've got a question regarding class inheritance. The following code reproduces the problem I'm dealing with: class IBase { public: virtual void Method(void)=0; };
17
2528
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and operator overloading and virtual functions. I mean it should not hamper the C++ meaning. In frank and simple terms i need to implement a small C++ compiler in C++, and i want the intermediate representation to be C. Please help me in this....
8
2609
by: acheron05 | last post by:
Hi there, Hopefully I won't fit the stereotype of the typical student posting his assignment to be written for him, but I am quite stuck. If anyone could give me some help in how to create dynamic objects (which inherit from a base class) and storing them in a vector, it would be absolutely wonderful at this point. I think from there I might be able to sculpt the rest of the program without (too much) trouble. I'll post what I've been...
4
1655
by: ramesh | last post by:
Why do we have a virtual destructor and why cant we have a virtual constructor?
3
2062
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual destructor? When I am defining such a class with default destructor then my compiler is giving warning that class XXX has virtual functions but non-virtual destructor.
4
1307
by: wizwx | last post by:
The followings are a few lines of code from the Template method in "Thinking in C++" vol.2 pp.639 class ApplicationFramework { protected: virtual void customize1() = 0; virtual void customize2() = 0; public: void templateMethod() { customize1();
4
2049
by: Anarki | last post by:
##include <iostream> using namespace std; class A { }; class B:virtual public A { }; class C:virtual public A
4
2059
by: Pranav | last post by:
Hello All, I have a simple question regarding the definition of abstract class, IIRC , Abstract class is one which contains virtual function declaration and other variables and no object of this class is created directly. If this is the case why don't we hide the constructor of abstract class into protected region? So that only object which inherits it can call the ctor of abstract class. In this way no one can create the object of...
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.