From: Rob Newberry <robnewberry@grouplogic.com>
To: Eddy Ilg <eddy@fericom.net>
Cc: gcc-help@gcc.gnu.org
Subject: Re: C++ - Calling virtual function from constructor
Date: Thu, 15 Nov 2001 18:19:00 -0000 [thread overview]
Message-ID: <Pine.LNX.4.10.10111221241550.12656-100000@host3.grouplogic.com> (raw)
In-Reply-To: <000c01c1735c$33131a80$6300a8c0@Freedom>
> Why can I not or how could I call a virtual function from the
> contstructor?
I don't know about the specifics of you're trying to do (you may be safe),
and I think it may depend on your compiler (and I don't know what g++
does), BUT...
The reason you don't call virtual methods inside a constructor is because
you don't know what kind of object you are -- while you're in the process
of being constructed, you're still in a _somewhat_ indeterminate state.
Only after you are constructed are you a real object.
Here's an example:
class a
{
public:
a();
virtual void v_meth();
};
class b : public a
{
public:
b();
virtual void v_meth();
};
a::a()
{
// this will _always_ call a::v_meth, because
// at this point, that's what we are -- even
// if we're in the process of constructing a
// subclass of class a
v_meth();
}
void a::v_meth()
{
printf("a::v_meth\n");
}
b::b()
{
// likewise, this will always call b::v_meth,
// because at this point, that's what we are
v_meth();
}
void b::v_meth()
{
printf("b::v_meth\n");
}
main()
{
printf("constructing an a:\n");
a an_a;
printf("constructing a b:\n");
b a_b;
}
The output of this, with gcc, is:
constructing an a:
a::v_meth
constructing a b:
a::v_meth
b::v_meth
As you can see, when you are constructing the 'b' object a_b, the
constructor for 'a' and 'b' get called in succession. But the constructor
for 'a' calls 'v_meth', it ONLY calls the 'a' method, because at that
point, that's all the object it is. This might not be the behavior you
want to have, since you're constructing a 'b' object. That's why the
compiler complains about you calling a virtual method in a constructor.
Rob
---------------------------------------------------------------------
Rob Newberry
Director of Fajita Technology
Group Logic, Inc.
next prev parent reply other threads:[~2001-11-22 18:01 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-11-14 22:45 Eddy Ilg
2001-11-15 18:19 ` Rob Newberry [this message]
2001-11-16 1:18 ` Frank Schafer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.10.10111221241550.12656-100000@host3.grouplogic.com \
--to=robnewberry@grouplogic.com \
--cc=eddy@fericom.net \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).