Using constructors vs two stage initialization

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Using constructors vs two stage initialization

Post by albinopapa » January 18th, 2019, 8:27 am

I'm not going to go into what each are aside from just stating my opinion on the matter and hope that someone can give me a reason why two stage initialization should even be a thing in C++.

C++ was designed to take care of allocating memory and initializing that memory when an object is declared and destructing and deallocating memory when the object goes out of scope if it was created on the stack. It's mechanisms for doing this are the constructors and destructor of a class. The constructor is called to do all the initialization of the object, while the destructor handles cleanup.

These functions are called for you by the compiler as sort of automatic lifetime management.

The copy assignment operator ( operator= ) is a function that can be overloaded in cases where things need to be handled before assigning more trivial data from one object to another, such as copying file data or image data before copying the simpler file size or image dimensions. The best part is, this function is called when you do your normal a = b, so no need to b = a.clone() or whatever ( with the exception of inheritance ).

So with these mechanisms in place, why do people feel the need to use Init() and Release() functions which have to be called manually?

Compare

Code: Select all

// Using constructor/destructor
int main()
{
    try
    {
        MyClass a( 420 );
        a.Run();

        a = MyClass( 42 );
        a.Run();
    }
    catch( const std::exception& e )
    {
        std::cout << e.what << '\n';
        return -1;
    }
    return 0;
}
Using two stage initialization

Code: Select all

int main()
{
    MyClass a;
    if( !a.Init( 420 ) )
    {
        a.Release();
        std::cout << "Unkown error." << '\n';
        return -1;
    }

    a.Run();

    MyClass copy;
    if( !copy.Init( 42 ) )
    {
        a.Release();
        copy.Release();
        std::cout << "Unknown error, unable to copy." << '\n';
        return -1;
    }

    a = copy;
    a.Run();
    a.Release();
    copy.Release();
    return 0;
}
More than likely, the Init function is going to have a boolean return, and for better error logging, you would handle the error inside the Init function or have a special function to return an error string.

In TSI ( two stage initialization ), you'd have to check that the 'copy' object was also successful, so you'd have to initialize it and release it manually.

So what brought this up?
The other day I got on YT and a video series was suggested to me, "C++ 3D Game Tutorial". It is a tutorial series and the author uses TSI.

Now, the author of the series seems to be a very nice person, so I'm not going to push too hard on the guy, after all he is delivering content I'm sure will help a few people break into making 3D games and I'm not going to hijack his channel in hopes people check out chili's content either. ( though I really wanted to hehe ).

Anyway, the long and short of it is, is the damn constructors and destructor for what they were meant.

Again, if anyone can explain to me why one would choose to use two stage initialization over constructors, please feel free.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Using constructors vs two stage initialization

Post by chili » January 20th, 2019, 11:01 pm

Yeah, I feel no need for Init() functions these days. As for initialization errors, exceptions provide a nice clean mechanism for that if you know how to use them. RAII is one of C++'s strengths imo, sad to see it neglected :(
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Using constructors vs two stage initialization

Post by albinopapa » January 21st, 2019, 4:15 am

chili wrote:... RAII is one of C++'s strengths imo, sad to see it neglected
Yes indeed.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply