 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kiran Guest
|
Posted: Fri Nov 19, 2004 3:36 pm Post subject: accessing virtulal function from constructor |
|
|
Hi,
I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
Thanks
Regards,
Kiran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
fabioppp Guest
|
Posted: Sat Nov 20, 2004 10:20 am Post subject: Re: accessing virtulal function from constructor |
|
|
A good explanation is given by Bruce Eckel in his first volume of
Thinking in C++.
Chapter 15. virtual functions & constructors.
--Fabio
Kiran wrote:
| Quote: | Hi,
I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Sat Nov 20, 2004 10:28 am Post subject: Re: accessing virtulal function from constructor |
|
|
"Kiran" <kiran.lokhande (AT) gmail (DOT) com> skrev i en meddelelse
news:2721ed0c.0411182128.73780258 (AT) posting (DOT) google.com...
| Quote: | Hi,
I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
Thanks
Regards,
Kiran
|
It is not barred, but you call the virtual function for the object that is
constructed:
struct b
{
virtual void f() { std::cout << "b"; }
b() { f();}
};
struct d: b
{
virtual void f() { std::cout << "d"; }
b() { f();}
};
void test()
{
d dd; // should produce "bd";
}
The reason is that the constructor of b can not call d's f as d is not
constructed yet.
Kind regards
Peter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Nov 20, 2004 10:35 am Post subject: Re: accessing virtulal function from constructor |
|
|
Kiran wrote:
| Quote: | I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
|
Nobody can explain why it's barred because it is _not_. If you have
problems with particular code, post your code and the error message
you're getting from your compiler.
The only thing that _might_ be bad is calling a _pure_ virtual function
from a constructor because that produces _undefined behaviour_.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Kurt Krueckeberg Guest
|
Posted: Sat Nov 20, 2004 4:23 pm Post subject: Re: accessing virtulal function from constructor |
|
|
| Quote: | Hi,
I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
Thanks
Regards,
Kiran
|
In C++ a derived object doesn't 'come to life' any it's constructor has been
run. If you want to be able to create copies of an object using only a base
class pointer, you could add a virtual clone method to the base class.
class base {
public:
//. . .
virtual base *clone() const = 0;
//. . .
};
class derived {
public:
derived *clone() const { return new(*this); }
// . . .
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sat Nov 20, 2004 6:15 pm Post subject: Re: accessing virtulal function from constructor |
|
|
Kiran wrote:
| Quote: | Hi,
I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
|
Virtual or pure virtual?
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sat Nov 20, 2004 6:21 pm Post subject: Re: accessing virtulal function from constructor |
|
|
Kiran wrote:
| Quote: | I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
|
It isn't, but its semantics are a bit surprising at first glance. The point
is, that inside the base-class' ctor, the object IS of type base-class,
i.e. both the static and dynamic type are base-class. That means that a
call to a virtual function will call the base-class' version thereof,
which will make problems when that function is pure virtual. Only once you
enter the ctor of the derived class, the type changes to the derived class
and calls to virtual functions also reach that class.
Note: the same applies to the destructor, just that the order is inverse.
Oh, and btw: if you say "gave me error", it would be helpful if you would
tell exactly what error and where, I'm just assuming a common case here
without knowing if it really applies to you.
Uli
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Mang Guest
|
Posted: Sat Nov 20, 2004 6:22 pm Post subject: Re: accessing virtulal function from constructor |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> schrieb im Newsbeitrag
news:Tuond.11175$Ae.3102 (AT) newsread1 (DOT) dllstx09.us.to.verio.net...
| Quote: | Kiran wrote:
I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
Nobody can explain why it's barred because it is _not_. If you have
problems with particular code, post your code and the error message
you're getting from your compiler.
The only thing that _might_ be bad is calling a _pure_ virtual function
from a constructor because that produces _undefined behaviour_.
|
I know what you mean, but for clarity you should add it is undefined
behaviour when the call is virtual.
You can call it using a qualified-id-syntax (which, of course, requires the
pure virtual function to be defined).
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Howard Lee Harkness Guest
|
Posted: Sun Nov 21, 2004 4:51 am Post subject: Re: accessing virtulal function from constructor |
|
|
[email]kiran.lokhande (AT) gmail (DOT) com[/email] (Kiran) wrote:
| Quote: | can anybody explain why accessing virtual function from constructor
is barred
|
Think about what is happening inside the ctor, and the purpose of the ctor. The
object is not fully constructed until the ctor exits successfully. That
includes things like vtbl entries.
--
Howard Lee Harkness, Licensed Insurance Agent
http://www.hlhins.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Loritsch Guest
|
Posted: Sun Nov 21, 2004 6:54 am Post subject: Re: accessing virtulal function from constructor |
|
|
[email]kiran.lokhande (AT) gmail (DOT) com[/email] (Kiran) wrote in message news:<2721ed0c.0411182128.73780258 (AT) posting (DOT) google.com>...
| Quote: | Hi,
I was doing something for
which I tried to access the virtual function
from constructor which gave me error.
can anybody explain why accessing virtual function from constructor
is barred
Thanks
Regards,
Kiran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
The basic answer lies in what a constructor does. Depending on the
compiler you are using, when an object is created something like the
following steps happens:
- Base class constructors and member constructors are called.
- A v-table is set up to allow correct binding of virtual function
calls.
- The body of the constructor is executed.
For example, consider the following (non-valid) code:
#include <iostream>
class Foo {
public:
Foo() {
this->bar();
}
~Foo() {}
virtual void bar() = 0;
};
class SpecialFoo : public Foo {
public:
SpecialFoo() : Foo() {}
virtual void bar() {
std::cout << "I am a SpecialFoon";
}
};
SpecialFoo specialFoo; //Create SpecialFoo object
Now, when a SpecialFoo object is constructed (as above), by looking at
the SpecialFoo() constructor, and the guidelines I outlined, you can
tell that the call to bar() doesn't make sense in this context.
The process is as follows:
When SpecialFoo's constructor is called, the first thing that happens
is that Foo's constructor is called. During the execution of Foo's
constructor, it doesn't know that it really ISA SpecialFoo. That
would require parent classes to have knowledge of their children.
For this reason, during the execution of Foo's constructor, the
SpecialFoo object does not yet exist. This means that the v-table of
the SpecialFoo object does not yet exist either, therefore the call to
bar() does not make any sense.
I hope this helps.
Michael Loritsch
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sun Nov 21, 2004 11:39 am Post subject: Re: accessing virtulal function from constructor |
|
|
Howard Lee Harkness wrote:
| Quote: | kiran.lokhande (AT) gmail (DOT) com (Kiran) wrote:
can anybody explain why accessing virtual function from constructor
is barred
Think about what is happening inside the ctor, and the purpose of the ctor. The
object is not fully constructed until the ctor exits successfully. That
includes things like vtbl entries.
|
You can call member functions labeled 'virtual' in the class
constructor, and you can tell exactly which functions they
are at compile time. This is different from what happens
after the object is fully constructed, but it is not illegal
nor forbidden.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Nov 21, 2004 11:42 am Post subject: Re: accessing virtulal function from constructor |
|
|
* Howard Lee Harkness:
| Quote: | kiran.lokhande (AT) gmail (DOT) com (Kiran) wrote:
can anybody explain why accessing virtual function from constructor
is barred
Think about what is happening inside the ctor, and the purpose of the ctor. The
object is not fully constructed until the ctor exits successfully.
|
Depending on the constructor in question an object _may_ not be fully
constructed until the constructor exits successfully, but it also may be
fully constructed before that; as a simple example, take any constructor
that already effects full construction, and add an output statement.
| Quote: | That includes things like vtbl entries.
|
That is incorrect. As others have pointed out, C++ does not bar you
from calling virtual functions from constructors. It is permitted.
The effect of doing so is a FAQ, and I direct readers to the FAQ on
that.
It's always a good idea to read (or at least, skim) the FAQ before
posting.
--
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?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|