public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ - Calling virtual function from constructor
@ 2001-11-14 22:45 Eddy Ilg
  2001-11-15 18:19 ` Rob Newberry
  2001-11-16  1:18 ` Frank Schafer
  0 siblings, 2 replies; 3+ messages in thread
From: Eddy Ilg @ 2001-11-14 22:45 UTC (permalink / raw)
  To: gcc-help

Hi,

I have a question that regards C++. In my program I have a class with some
virtual functions. One virtual Function does some initialization specific to
the derived class. It looks someThing like this:

class scmObject
{
  protected:
    virtual void    construct()=0;
  public:
    scmObject();
}

scmObject::scmObject()
{
  ...
  construct();
}

When I try to compile this I get:
scmObject.cpp: In method `scmObject::scmObject(BRANCH *)':
scmObject.cpp:50: abstract virtual `void scmObject::construct(BRANCH *)'
called from constructor

When I chage it like this I can compile it:

class scmObject
{
  protected:
    virtual void    construct()=0;
    void     init();
public:
    scmObject();
}

scmObject::scmObject()
{
  ...
  init();
}

void scmObject::init()
{
  construct();
}

Why can I not or how could I call a virtual function from the contstructor?


Thanks


Eddy

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: C++ - Calling virtual function from constructor
  2001-11-14 22:45 C++ - Calling virtual function from constructor Eddy Ilg
@ 2001-11-15 18:19 ` Rob Newberry
  2001-11-16  1:18 ` Frank Schafer
  1 sibling, 0 replies; 3+ messages in thread
From: Rob Newberry @ 2001-11-15 18:19 UTC (permalink / raw)
  To: Eddy Ilg; +Cc: gcc-help


> 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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: C++ - Calling virtual function from constructor
  2001-11-14 22:45 C++ - Calling virtual function from constructor Eddy Ilg
  2001-11-15 18:19 ` Rob Newberry
@ 2001-11-16  1:18 ` Frank Schafer
  1 sibling, 0 replies; 3+ messages in thread
From: Frank Schafer @ 2001-11-16  1:18 UTC (permalink / raw)
  To: Eddy Ilg; +Cc: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1228 bytes --]

Eddy Ilg wrote:

> Hi,
>
> I have a question that regards C++. In my program I have a class with some
> virtual functions. One virtual Function does some initialization specific to
> the derived class. It looks someThing like this:
>
> class scmObject
> {
>   protected:
>     virtual void    construct()=0;
>   public:
>     scmObject();
> }
>
> scmObject::scmObject()
> {
>   ...
>   construct();
> }
>
> When I try to compile this I get:
> scmObject.cpp: In method `scmObject::scmObject(BRANCH *)':
> scmObject.cpp:50: abstract virtual `void scmObject::construct(BRANCH *)'
> called from constructor
>
> When I chage it like this I can compile it:
>
> class scmObject
> {
>   protected:
>     virtual void    construct()=0;
>     void     init();
> public:
>     scmObject();
> }
>
> scmObject::scmObject()
> {
>   ...
>   init();
> }
>
> void scmObject::init()
> {
>   construct();
> }
>
> Why can I not or how could I call a virtual function from the contstructor?
>
> Thanks
>
> Eddy

Hi,

seems you found the reason yoyrself ;-) ... and the early stage fault is ... (
see the C++ standard ) ... you CAN call any virtual function, but you CAN'T
call an abstract function ( the ...=0 ) from the constructor.

Regards
Frank

[-- Attachment #2: Card for Frank Schafer --]
[-- Type: text/x-vcard, Size: 218 bytes --]

begin:vcard 
n:Schafer;Frank
x-mozilla-html:FALSE
org:SETUZA a.s.;IT
adr:;;;;;;
version:2.1
email;internet:frank.schafer@setuza.cz
title:Dipl. Ing.
note:System administrator
x-mozilla-cpt:;0
fn:Frank Schafer
end:vcard

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-11-23  7:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-14 22:45 C++ - Calling virtual function from constructor Eddy Ilg
2001-11-15 18:19 ` Rob Newberry
2001-11-16  1:18 ` Frank Schafer

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).