473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2005

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
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
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...
5
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
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();...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
5
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...
8
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...
11
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...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.