cancel a constructor ?

F

Flzw

Is it possible to cancel a constructor ?

I mean, for example I have an object which is created by reading a file so
there is a filename argument to the constructor. But if it happens the file
is not of the proper type the constructor should fail and not create any
object.
 
V

Victor Bazarov

Flzw said:
Is it possible to cancel a constructor ?

I mean, for example I have an object which is created by reading a file so
there is a filename argument to the constructor. But if it happens the file
is not of the proper type the constructor should fail and not create any
object.

Yes, throw an exception.

V
 
M

Mike Wahler

Flzw said:
Yes, thanks, just read some things about that, but, what happens to the
members variables ? are they destroyed properly ?

Yes. But if any of them are of UDT's make sure their
destructors do what is needed to 'clean up' if necessary.

-Mike
 
I

Ioannis Vranos

Flzw said:
Yes, thanks, just read some things about that, but, what happens to the
members variables ? are they destroyed properly ?


If you have allocated objects in the free store using new beforehand
within the constructor (usually a bad idea to do it in this way), make
sure to clean them up before throwing the exception.
 
M

msalters

Ioannis said:
If you have allocated objects in the free store using new beforehand
within the constructor (usually a bad idea to do it in this way), make
sure to clean them up before throwing the exception.

That's one example of cleanup you'd have to do. Furthermore, if you had
C-style FILE*'s, you would need to fclose() them, etcetera. Basically,
you would do whatever you would normally do in the destructor.

The easiest way is to use C++-style members: std::fstream will clean
up itself unlike fclose(FILE*), std::string will clean up instead of
free(char*), etcetera. If you have another resource which needs
C-style cleanup (e.g. from an legacy API), you can wrap that
resource in a C++ class, with a dtor dedicated to this cleanup
(many early std::fstream implementations were such wrappers around
FILE*)

The trickier way is to write a function try-block which wraps
the entire ctor (including member initializers). Check your favorite
C++ book for that.

Regards,
Michiel Salters
 
I

Ioannis Vranos

msalters said:
That's one example of cleanup you'd have to do. Furthermore, if you had
C-style FILE*'s, you would need to fclose() them, etcetera. Basically,
you would do whatever you would normally do in the destructor.

The easiest way is to use C++-style members: std::fstream will clean
up itself unlike fclose(FILE*), std::string will clean up instead of
free(char*), etcetera. If you have another resource which needs
C-style cleanup (e.g. from an legacy API), you can wrap that
resource in a C++ class, with a dtor dedicated to this cleanup
(many early std::fstream implementations were such wrappers around
FILE*)

Exactly.


The trickier way is to write a function try-block which wraps
the entire ctor (including member initializers). Check your favorite
C++ book for that.


I already knew that! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top