473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

cancel a constructor ?

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.
Jul 22 '05 #1
6 2660
Flzw wrote:
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
Jul 22 '05 #2

Yes, throw an exception.


Yes, thanks, just read some things about that, but, what happens to the
members variables ? are they destroyed properly ?
Jul 22 '05 #3
"Flzw" <fl****@wanadoo.fr> wrote in message
news:41***********************@news.wanadoo.fr...

Yes, throw an exception.


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
Jul 22 '05 #4
Flzw wrote:
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.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5

Ioannis Vranos wrote:
Flzw wrote:
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.


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

Jul 22 '05 #6
msalters wrote:
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! :-)


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: AP | last post by:
Hi, I'm trying to use c# to pop up a dialog box when a user attempts to close word to prompt them if they want to exit or cancel (obviously other stuff needs to happen based on their selection...
2
by: pei_world | last post by:
I have a Window form app, How can I stop its exiting... thanks
13
by: Mike L | last post by:
I have a child form frmDataEntry call up another child form frmDealerSearch. If the user clicks on cancel on frmDealerSearch, I want to close frmDealerSearch and put the focus on txtDealerNum on...
14
by: clintonG | last post by:
This is an appeal for peer support sent to Microsoft as will be noted in closing. The Login control does not include a Cancel button. The only option is to convert the Login control to a...
4
by: Eric | last post by:
Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times. String Constructor (Char, Int32) Why am I getting...
7
by: Pieter | last post by:
Hi, Most of my Objects have a constructor like this "Public Sub New(ID as Integer)". But when this ID isn't valid, I end up with an instantiated object, that doesn't contain values. Is there a...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I change the confirm box to say yes/no or default to cancel?...
16
by: parez | last post by:
I start a BackGroundWorker to populate a grid. It is started off in the ui layer The thread follows( cannot think of a better word) the path UI->Layer1->Layer2->Communication Layer and it...
5
by: ghjk | last post by:
I have "cancel" button in php files. I want to write common javascript function for cancel button. When user click cancel button I want to clear php form data. Is it possible? <input id="Cancel"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.