Re: Using Reference of Pointers

niklasb_at_microsoft.com
Date: 02/17/05

  • Next message: dmitry.freitor_at_citigroup.com: "Why call member function through this pointer?"
    Date: 17 Feb 2005 12:03:50 -0800
    
    

    V. de Bellabre wrote:
    > Some of my c++ class are complex and use many pointers so I decided
    to
    > force initialisation of objects through their static function Create,
    > which has a parameter IN-OUT that will contain the instance of the
    > created object.

    An alternative would be to simply have your Create function return
    a pointer. OUT parameters have their place, but all else being equal,
    simply returning a value is usually more clear.

    > typedef class Texture* PTEXTURE;
    > // With this, PTEXTURE& is a reference of a pointer to Texture

    Personally, I think hiding a pointer behind a typedef like this
    obfuscates more than it clarifies. Yes, I know the Windows header
    files do this all the time, but I suspect that's a holdover from
    the bad old 16-bit days of __near and __far pointers.

    > class Texture:
    > {
    > protected:
    > Texture( void ){} // Constructor, do nothing, use Create

    Is Texture meant to be derived from? If so, you should probably
    add a virtual destructor. If not, the ctor should be private
    instaed of protected.

    > public:
    > static void Create( PTEXTURE& ); // PTEXTURE& is a reference of a
    > pointer to the texture to create

    Or:

      static Texture* Create();

    Problem solved, and no need for reference-to-pointer.

    > // Implementation of the Create Function
    > void Texture::Create( PTEXTURE& pTexToCreate )
    > {
    > pTexToCreate = new Texture; // memory alloc
    >
    > // Some init...
    >
    > if( badInit )
    > {
    > delete pTexToCreate;
    > pTexToCreate = NULL;
    > }
    > }

    The code above should work as you intended. The only thing I
    can think of is there's undefined behavior somewhere else in
    code you snipped. You might try taking the stripped-down code
    you posted and edit it to be actually compilable, then run it
    to see if the problem still manifests.

    As a matter of style, I'd prefer to see Create return a
    pointer. There's no need for an OUT or IN/OUT parameter here.

    Better yet, why not do the initialization in the ctor as C++
    is meant to be used? If initialization failed, the ctor would
    throw an exception. (My guess is you're using a factory method
    instead because you want to return NULL instead of throwing an
    exception on failure.)

    > // A small example of how I use this
    > void main( void )
    > {
    > PTEXTURE pMyTexture;
    > Texture::Create( pMyTexture );
    > if( pMyTexture == NULL )
    > // Error creating the texture
    > }

    The return value of main must be int. The above is not legal
    C++, although some compilers accept it.


  • Next message: dmitry.freitor_at_citigroup.com: "Why call member function through this pointer?"

    Relevant Pages

    • RE: Value class initialization rules
      ... it is not a requirement for a valuetype .ctor to initialize every field. ... >Thread-Topic: Value class initialization rules ... I found many paragraphs where the value types have a "this" pointer ...
      (microsoft.public.dotnet.framework.clr)
    • Re: OT: Requesting C advice
      ... some behind the scenes action of the compiler. ... In fact the memory could ... Proper initialization means that floats and doubles must be initialized to 0.0 and pointers must be initialized to the null pointer value, even if those bit patterns differ from all-bits-zero. ...
      (Fedora)
    • Re: Initialization of character array in derived type
      ... In fact, if the array is large enough for the time to be measurable, ... be faster than doing it with static initialization. ... I see that your init-mat_data is a pointer. ... allocate or assign the pointer somewhere. ...
      (comp.lang.fortran)
    • Re: why cant functions return arrays
      ... at initialization. ... I think you don't understand your own compiler. ... but you have not specified how overloading works with arrays unless I ... Pointer + integer ...
      (comp.compilers.lcc)
    • Re: [PATCH 00/11] Task watchers: Introduction
      ... notify_watchers() before any other watchers are called for the new task. ... The use of an array of function pointers can cut ... Within the array a null pointer would mean "don't bother ... to visit all existing tasks and do whatever initialization it needs to ...
      (Linux-Kernel)

    Loading