Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Call virtua...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 26 Topic 43354 of 48417
Post > Topic >>

Re: Call virtual function in constructor

by red floyd <no.spam@[EMAIL PROTECTED] > Feb 16, 2008 at 07:33 PM

junw2000@[EMAIL PROTECTED]
 wrote:
> Is there any problem if call virtual function in constructor? For
> example:
> 
> class A
> {
> public:
>      A()
>      {
>            setvalue();
>      }
> 
>     setvalue()
>     {
>            data = getnumber();
>     }
> 
>     print() { cout << data << endl;
> 
> private:
>     virtual int getnumber() {return 100;}
>     int data;
> }
> 
> class B : public A
> {
> public:
>          B() { }
> 
> private:
>          virtual int getnumber() {return 22222;}
> 
> }
> 
> int main()
> {
>      A test1;
>      test1.print() ;
> 
>      B test2;
>      test2.print() ;
> 
> }
> 
> Thanks.
>

Yes, there's a problem.  When you construct a B, at the time the virtual 
function is called from A's constructor, the B hasn't been constructed 
yet, so you get either A's version or undefined behavior (I can't 
remember which, and my copy of the Standard is at work).
 




 26 Posts in Topic:
Call virtual function in constructor
junw2000@[EMAIL PROTECTED  2008-02-16 11:25:25 
Re: Call virtual function in constructor
red floyd <no.spam@[EM  2008-02-16 19:33:02 
Re: Call virtual function in constructor
Ian Collins <ian-news@  2008-02-17 08:37:36 
Re: Call virtual function in constructor
Ron Natalie <ron@[EMAI  2008-02-23 20:36:47 
Re: Call virtual function in constructor
Ian Collins <ian-news@  2008-02-24 15:05:51 
Re: Call virtual function in constructor
peter koch <peter.koch  2008-02-16 11:48:01 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-16 21:40:51 
Re: Call virtual function in constructor
Kira Yamato <kirakun@[  2008-02-16 17:12:42 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-16 23:02:00 
Re: Call virtual function in constructor
"Alf P. Steinbach&qu  2008-02-17 00:37:18 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-17 01:53:08 
Re: Call virtual function in constructor
"Alf P. Steinbach&qu  2008-02-17 04:49:03 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-17 15:06:02 
Re: Call virtual function in constructor
"Alf P. Steinbach&qu  2008-02-18 03:35:45 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-18 06:45:25 
Re: Call virtual function in constructor
"Alf P. Steinbach&qu  2008-02-18 08:54:48 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-18 17:07:48 
Re: Call virtual function in constructor
"Alf P. Steinbach&qu  2008-02-18 18:52:12 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-18 18:42:39 
Re: Call virtual function in constructor
"Alf P. Steinbach&qu  2008-02-19 02:44:08 
Re: Call virtual function in constructor
"Alf P. Steinbach&qu  2008-02-19 05:12:14 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-20 05:53:34 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-20 05:17:51 
Re: Call virtual function in constructor
Kira Yamato <kirakun@[  2008-02-16 19:05:20 
Re: Call virtual function in constructor
Pavel <dot_com_yahoo@[  2008-02-17 02:17:51 
Re: Call virtual function in constructor
James Kanze <james.kan  2008-02-17 03:17:45 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 10:21:34 CST 2008.