Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7366

Re: Virtual functions in constructors. Is it possible?

From "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Virtual functions in constructors. Is it possible?
Date 2011-07-02 11:17 +0200
Organization A noiseless patient Spider
Message-ID <iumn7a$veh$1@dont-email.me> (permalink)
References <iulh53$8t7$1@speranza.aioe.org> <iulhlm$tji$2@dont-email.me> <iulkmt$fvq$1@speranza.aioe.org> <iumejo$kv6$1@dont-email.me> <iumm5c$di7$1@speranza.aioe.org>

Show all headers | View raw


* Anonymous, on 02.07.2011 10:50:
> Alf P. Steinbach /Usenet ha scritto:
>> * Anonymous, on 02.07.2011 01:19:
>>>> See the FAQ
>>>>
>>>> <url: http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6>
>>>
>>> That is an ugly solution in my opinion.
>>
>> It's not "a" solution, it's a discussion of the possible ways in current C++.
>
> Ok, to be precise, the ways mentioned in the FAQ have the following limitations:
>
> "In this variation, the code that is creating the object *explicitly executes
> both phases*. When executing Phase I, the code creating the object either knows
> the object's exact class (e.g., new Derived() or perhaps a local Derived
> object), or doesn't know the object's exact class (e.g., the virtual constructor
> idiom or some other factory). The "doesn't know" case is strongly preferred when
> you want to make it easy to plug-in new derived classes."
>
> and
>
>
> "The next approach does not rely on a two-phase initialization, instead using a
> second hierarchy whose only job is to house methods foo() and bar(). *This
> approach doesn't always work, and in particular it doesn't work in cases when
> foo() and bar() need to access the instance data declared in Derived*, but it is
> conceptually quite simple and clean and is commonly used."
>
> In a way or the other static member functions are used there, which make the
> approaches just "ugly".
>
> They key to the *perfect solution* is to profit by the lifetime of temporary
> objects passed to constructors: you can invoke a virtual function init() after
> an object has been constructed by calling it from a destructor of a temporary
> object passed to the constructor of the class that has to be initialized.

So, if I do

   p = new YourClass()

and init() fails, do I have a memory leak or not?

But I think the scheme that you're suggesting should be in that FAQ item.

I didn't think of it when I convinced Marshall to add that FAQ item (and neither 
did he, or AFAIK anyone else), sorry.



Cheers & thanks,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 00:19 +0200
  Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-02 00:37 +0200
    Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 01:19 +0200
      Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-02 08:50 +0200
        Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 10:50 +0200
          Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-02 11:17 +0200
            Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 11:25 +0200
              Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 11:31 +0200
              Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-02 12:00 +0200
              Re: Virtual functions in constructors. Is it possible? Werner <werasm@gmail.com> - 2011-07-04 01:10 -0700
                Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-04 20:41 +0200
                Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-04 21:35 +0200
                Re: Virtual functions in constructors. Is it possible? Leo Equinox Gaspard <ekinox1995@gmail.com> - 2011-07-04 23:59 +0200
                Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-05 11:09 +0200
                Re: Virtual functions in constructors. Is it possible? Leo Equinox Gaspard <ekinox1995@gmail.com> - 2011-07-05 13:15 +0200
                Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-05 14:51 +0200
                Re: Virtual functions in constructors. Is it possible? Leo Equinox Gaspard <ekinox1995@gmail.com> - 2011-07-05 19:55 +0200
                Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-05 20:46 +0200
                Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-05 21:23 +0200
                Re: Virtual functions in constructors. Is it possible? Werner <werasm@gmail.com> - 2011-07-06 00:14 -0700
  Re: Virtual functions in constructors. Is it possible? Virchanza <virtual@lavabit.com> - 2011-07-05 14:58 -0700
    Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-06 01:28 +0200

csiph-web