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


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

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-05 21:23 +0200
Organization A noiseless patient Spider
Message-ID <iuvo43$bb$1@dont-email.me> (permalink)
References (10 earlier) <iutd2n$d9m$1@dont-email.me> <iuuk44$i31$1@dont-email.me> <iuurn9$sck$1@dont-email.me> <iuv17c$uog$1@dont-email.me> <iuvj59$s8m$1@dont-email.me>

Show all headers | View raw


* Leo Equinox Gaspard, on 05.07.2011 19:55:
> <code>
> #include <iostream>
> #include <stdexcept>
> #include <cstdlib>
> #include <memory>
> using namespace std;
>
> namespace g {
>      int nObjectsAllocated  = 0;
> }    // namespace g
>
> class VirtualBase {
> protected:
>      class Initializer;
>      friend class Initializer;
>
>      class Initializer {
>      public:
>
>          Initializer() {
>              cout << "Initializer" << endl;
>              p = 0;
>          }
>
>          ~Initializer() {
>              cout << "~Initializer" << endl;
>              try {
>                  if (p)
>                      p->Init(); // call the virtual function
>              } catch (...) {
>                  p->InitFailed();
>                  throw;
>              }
>          }
>          // might be private if VirtualBase is declared as friend...
>
>          void Register(VirtualBase* b) const {
>              p = b;
>          }
>      private:
>          mutable VirtualBase* p;
>          // private and not implemented
>          Initializer(const Initializer&);
>          void operator =(const Initializer&);
>      };
> public:
>
>      VirtualBase(const Initializer& i) {
>          cout << "VirtualBase" << endl;
>          i.Register(this);
>      }
> private:
>      virtual void Init() = 0;
>      // will be called immediately after the constructor
>      // of the most derived class
>
>      virtual void InitFailed() = 0;
>      // will be called if Init failed. Should leave the object
>      // in a destructable state.
> };
>
> // This is the actual hierarchy
>
> class Base : public virtual VirtualBase {
> public:
>
>      static void* operator new( size_t size )
>      {
>          cout << "Allocation" << endl;
>          ++g::nObjectsAllocated;
>          return ::operator new( size );
>      }
>
>      static void operator delete( void* p )
>      {
>          cout << "Deallocation" << endl;
>          --g::nObjectsAllocated;
>          return ::operator delete( p );
>      }
>
>      Base(const VirtualBase::Initializer& i = Initializer()) : VirtualBase(i) {
>          cout << "Base" << endl;
>      }
>
>      ~Base() {
>          cout << "~Base" << endl;
>      }
>
>      void Init() {
>          cout << "Base::Init()" << endl;
>          #ifdef FAIL_INIT
>              throw runtime_error( "Oops, `Init` failed." );
>          #endif
>      }
>
>      void InitFailed() {
>          cout << "Base::InitFailed() - could do something if needed" << endl;
>      }
> };
>
> int main()
> {
>      int result = EXIT_FAILURE;
>      try
>      {
>          auto_ptr<Base> p(new Base);  // calls Base::Init.
>          // We *need* an auto_ptr to ensure that the item will be
>          // deleted in case of exception.
>          result = EXIT_SUCCESS;
>      }
>      catch( exception const& x )
>      {
>          cerr << "!" << x.what() << endl;
>      }
>
>      if( g::nObjectsAllocated > 0 )
>      {
>          cerr << "!Memory leak." << endl;
>      }
>      cout << "Finished." << endl;
>      return result;
> }
> </code>
>
> So I must repeat my question : Where am I mistaking ?

Your code

* has Undefined Behavior if a constructor throws, and
* leaks memory without the auto_ptr

(while there's no leak for an ordinary single stage construction, because the 
new-expression then cleans up).

The UB is critical, so this technique is only applicable for guaranteed no-throw 
constructors, which except for default constructors are pretty rare beasts nowadays.

I don't know what you think you're mistaken about, sorry, but hopefully it's one 
of the two points above?


Cheers & hth.,

- 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