Topic: constructor & memberfunction
Author: Frank Uepping <Frank.Uepping@epost.de>
Date: Sat, 26 Jan 2002 22:40:04 GMT Raw View
Permits the standard the calling of a member-function from a constructor?
E.g.:
class X
{
public:
X () { _init (); }
private:
void _init () { /* Work with member variables! */ }
};
Regards
FAU
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Marek Przeczek <marek.przeczek@st.ms.mff.cuni.cz>
Date: Mon, 28 Jan 2002 00:14:36 GMT Raw View
Frank Uepping wrote:
> Permits the standard the calling of a member-function from a constructor?
> E.g.:
>
> class X
> {
> public:
> X () { _init (); }
>
> private:
> void _init () { /* Work with member variables! */ }
> };
>
> Regards
> FAU
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
it seems okay, the problem is when _init() is virtual method called
from constructor, because in time when constructor's code runs, virtual
table may not be initialized yet. but if you only call a non-virtual
method which works with variables only (and doesn't call another virtual
method of the same class X), i don't see the problem...
and i think a standard says the same about calling methods from constructor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Mon, 28 Jan 2002 18:27:06 GMT Raw View
Marek Przeczek wrote:
>
> Frank Uepping wrote:
>
> > Permits the standard the calling of a member-function from a constructor?
> > E.g.:
> >
> > class X
> > {
> > public:
> > X () { _init (); }
> >
> > private:
> > void _init () { /* Work with member variables! */ }
> > };
> >
> > Regards
> > FAU
....
> it seems okay, the problem is when _init() is virtual method called
> from constructor, because in time when constructor's code runs, virtual
> table may not be initialized yet. but if you only call a non-virtual
> method which works with variables only (and doesn't call another virtual
> method of the same class X), i don't see the problem...
The standard doesn't say anything about the virtual table, but does
require that virtual functions may be called during a constructor.
Therefore, if a virtual function table pointer is used, it must be
installed in time to be used for any such call. However, the key point
is which virtual function table is to be installed. It must be the table
for the class that is currently under construction; the object is not
yet of any type that is derived from that class. Examples:
class Base {
virtual void func()=0;
Base() {
func(); // calls Base::func()
}
};
Base::func() { }
class Derived: public Base {
func();
Derived() {
func(); // calls Derived::func()
}
}
class Leaf: public Derived {
Leaf() {
func(); // calls Derived::func()
}
};
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]