473,623 Members | 3,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual keyword & compile time

dc
Can anybody think of a situation where virtual function's address
resolution/ something related to virtual is done at compile time

Jan 11 '06 #1
7 2014

dc wrote:
Can anybody think of a situation where virtual function's address
resolution/ something related to virtual is done at compile time


1. Virtual functions called inside constructor and destructor are
statically bound.
2. If a virtual function is called on the _object_ instead of calling
on _pointer_ or _reference_, it is statically bound.

Of course, the fact that it is statically bound has nothing to do with
the resolution being done at compile time, but smart compilers can take
advantage of this and actually do the resolution at compile time.

Jan 11 '06 #2
Neelesh Bodas wrote:

dc wrote:
Can anybody think of a situation where virtual function's address
resolution/ something related to virtual is done at compile time


1. Virtual functions called inside constructor and destructor are
statically bound.


AFAIK, they are still dynamically bound, but only up to the class that the
constructor/destructor belongs to. The effect is the same for functions
called directly, but if called indirectly (i.e. from a member function that
is called from the constructor or destructor), there is a difference.

Btw: Is there a common word for both constructors and destructors so that
one doesn't have to mention both each time? I once read "structors"
somewhere, but it looks strange to me and I'm not sure it's widely
understood.

Jan 11 '06 #3

Rolf Magnus wrote:
Neelesh Bodas wrote:

dc wrote:
Can anybody think of a situation where virtual function's address
resolution/ something related to virtual is done at compile time


1. Virtual functions called inside constructor and destructor are
statically bound.


AFAIK, they are still dynamically bound, but only up to the class that the
constructor/destructor belongs to. The effect is the same for functions
called directly, but if called indirectly (i.e. from a member function that
is called from the constructor or destructor), there is a difference.


Rereading my post after your reply, I think that I used completely
wrong words - "statically bound" , since by definition, "statically
bound" means resolved at compile time whereas dynamically "bound means"
resolved at run time -- (Please correct me if I am wrong).

I wanted to say that the virtual function (for the same object ) called
inside the constructor or destructors (the "structors" ) are bound
locally - ie. the version of the same class. Similar case is with the
virtual functions being called on object rather than a pointer or
referece.

Please correct me in case thats not what actually happens.

Jan 11 '06 #4
Neelesh Bodas wrote:
> dc wrote:
>> Can anybody think of a situation where virtual function's address
>> resolution/ something related to virtual is done at compile time
>
> 1. Virtual functions called inside constructor and destructor are
> statically bound.


AFAIK, they are still dynamically bound, but only up to the class that
the constructor/destructor belongs to. The effect is the same for
functions called directly, but if called indirectly (i.e. from a member
function that is called from the constructor or destructor), there is a
difference.


Rereading my post after your reply, I think that I used completely
wrong words - "statically bound" , since by definition, "statically
bound" means resolved at compile time whereas dynamically "bound means"
resolved at run time -- (Please correct me if I am wrong).

I wanted to say that the virtual function (for the same object ) called
inside the constructor or destructors (the "structors" ) are bound
locally - ie. the version of the same class. Similar case is with the
virtual functions being called on object rather than a pointer or
referece.

Please correct me in case thats not what actually happens.


Well, as I said, for functions called directly from within the constructor
or destructor, the effect is the same. But consider this example:

#include <iostream>

struct Base
{
virtual void hello()
{
std::cout << "Hello, world, I'm a Base\n";
}

void test()
{
hello();
}
};

struct Derived
{
virtual void hello()
{
std::cout << "This is Derived saying Hello\n";
}

Derived()
{
test();
}
};

int main()
{
Derived();
}

Of course, you can extend that example to a bigger hierarchy. The bottom
line is that the virtual functions get indeed resolved dynamically, but
only up to the class that the constructor belongs to. If Base::test() gets
inlined, the compiler has a good chance of resolving the call at compile
time, but otherwise it must defer it until runtime.

Jan 11 '06 #5
Ok, forgot to test the code before posting, and of course, I forgot
something.

Rolf Magnus wrote:
#include <iostream>

struct Base
{
virtual void hello()
{
std::cout << "Hello, world, I'm a Base\n";
}

void test()
{
hello();
}
};

struct Derived
: public Base
{
virtual void hello()
{
std::cout << "This is Derived saying Hello\n";
}

Derived()
{
test();
}
};

int main()
{
Derived();
}

Jan 11 '06 #6
On Wed, 11 Jan 2006 15:53:15 +0100, Rolf Magnus <ra******@t-online.de>
wrote:
Well, as I said, for functions called directly from within the constructor
or destructor, the effect is the same. But consider this example:

#include <iostream>

struct Base
{
virtual void hello()
{
std::cout << "Hello, world, I'm a Base\n";
}

void test()
{
hello();
}
};

struct Derived
{
virtual void hello()
{
std::cout << "This is Derived saying Hello\n";
}

Derived()
{
test();
}
};

int main()
{
Derived();
}


You forgot to inherit from Base in class Derived (oops!). For anybody
trying to learn from this code, it should actually be:

#include <iostream>

struct Base
{
virtual void hello()
{
std::cout << "Hello, world, I'm a Base\n";
}

void test()
{
hello();
}
};

struct Derived : public Base
{
virtual void hello()
{
std::cout << "This is Derived saying Hello\n";
}

Derived()
{
test();
}
};

int main()
{
Derived();
}
Jan 11 '06 #7
dc
Thanks all for ur inputs.

That means virtual functions are never resolved during compile time
even when called directly from with in constructor/destructor or
invoked with object ( ie neither ptr nor reference.)

Plz correct if I am wrong..

Jan 12 '06 #8

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

Similar topics

2
8024
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
20
1768
by: Raymond Lewallen | last post by:
I read this on this website page http://www.vbip.com/books/1861004915/chapter_4915_06.asp: Unlike many object-oriented languages, all methods in VB.NET are virtual. Now in BOL, Under Perforamce Tips and Tricks in .NET Applications, A .Net Developer Platform White Paper, at the very bottom, it says: The JIT cannot inline virtual methods, so you lose a potential optimization if you get rid of non-virtual methods.
5
1613
by: gouqizi.lvcha | last post by:
Hi, all: I have 3 class X, Y, Z class Y is a subclass of class X; class Z is a subclass of class Y; i.e. class Y : public class X class Z : public class Y
28
5199
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
32
4503
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
5
2605
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two classes yClass which inherits xClass. xClass has a virtual method which simply writes a line of text stating its origin, yClass implements the same method which writes a line of text stating its origin also (i.e. "From yClass"). I ran the...
8
2888
by: rakoo | last post by:
I want to question about this virtual keyword , what is neccessty of it .. when base class ponter or simply object assingned to derived class object ,we never want that base class funtion by base class object or pointer which pointing to derived class object Are we ? then why stroupstrop has given this keyword
11
3420
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the vtable pointer is made use of.. eg. class one {
14
4192
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
8662
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
8463
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
7134
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
6104
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
5560
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4067
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...
1
2593
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1468
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.