1

Topic: How in the designer of object to refuse its creation?

I create object in which storage (a safe array) is dynamic selected. And so, without successful selection of storage to create object senselessly. How in the designer in case of unsuccessful selection to refuse object creation?
There are any patterns of programming in such situations?

2

Re: How in the designer of object to refuse its creation?

Exception to throw

throw std:: bad_alloc;

Or to remove selection of storage from the designer in a separate method and to return therefrom it was possible to select storage or not.

3

Re: How in the designer of object to refuse its creation?

Dima T wrote:

the Exception to throw

throw std:: bad_alloc;

And it is possible in Qt in the code to throw only exceptions, and somewhere in one place all them to intercept and on parameter to understand what has happened?
Simply long to write all these cathc (int i).

4

Re: How in the designer of object to refuse its creation?

And it already it is necessary to look that you are going to do if storage selection broke off and
The object did not form.
PS: For fingers be not afraid, a skin .

5

Re: How in the designer of object to refuse its creation?

Dimitry Sibiryakov,
I should make an identical dial-up of actions (to save the intermediate calculations) at any exceptions, and then to quit the program. Therefore it would be healthy, if in a body of the program I simply throw "throw 3;" or "throw 4;", and in an exception handling platitude caused the function for saving of the intermediate values.

6

Re: How in the designer of object to refuse its creation?

AlekseySQL wrote:

Dimitry Sibiryakov,
I should make an identical dial-up of actions (to save the intermediate calculations) at any exceptions, and then to quit the program. Therefore it would be healthy, if in a body of the program I simply throw "throw 3;" or "throw 4;", and in an exception handling platitude caused the function for saving of the intermediate values.

The exception causes  already created objects, there save.
PS In remaining a question "as it is correct" has no unambiguous answer since it is a designing question, i.e. from a category of taste of felt-tip pens.
PPS I should not be asked as in my opinion correctly, since I for return codes, i.e. the second part of my post

7

Re: How in the designer of object to refuse its creation?

Dima T wrote:

I for return codes

I will be corrected: I for error status code resetting.

8

Re: How in the designer of object to refuse its creation?

AlekseySQL wrote:

And it is possible in Qt in the code to throw only exceptions, and somewhere in one place all them to intercept and on parameter to understand what has happened?

Qt basically does not work with exceptions. Their this decision. Your code should not throw out an exception in calls Qt. ., for example, you should not take off from the slot on an exception, should not take off on an exception of any overloaded method Qt etc.
There are details, it is possible to install the global handler of messages Qt and there to deliver the catch (...), but here I do not remember.

9

Re: How in the designer of object to refuse its creation?

That concerning an initial question. In a C ++ to refuse object creation it is possible only one method: to throw out from the designer an exception. But here there are details. For example:

Foo:: Foo ()
{
size_t const FooSize = 128;
m_data = new char [FooSize] ();
//Further something is not pleasant to us.
throw std:: exception ();
//Everything, now the data package on which specifies m_data is lost.
}

As with such to struggle - an individual question.

10

Re: How in the designer of object to refuse its creation?

AlekseySQL wrote:

I create object in which storage (a safe array) is dynamic selected. And so, without successful selection of storage to create object senselessly. How in the designer in case of unsuccessful selection to refuse object creation?
There are any patterns of programming in such situations?

To throw an exception.

11

Re: How in the designer of object to refuse its creation?

petrav wrote:

[quot
Qt basically does not work with exceptions. Their this decision. Your code should not throw out an exception in calls Qt. ., for example, you should not take off from the slot on
That?
We use completely exceptions, both in a tail, and in a mane. All apprx.
Generally, the modern application on With ++ should use exceptions.

12

Re: How in the designer of object to refuse its creation?

MasterZiv;
http://doc.qt.io/qt-5/exceptionsafety.h … -and-slots

13

Re: How in the designer of object to refuse its creation?

AlekseySQL wrote:

I create object in which storage (a safe array) is dynamic selected. And so, without successful selection of storage to create object senselessly. How in the designer in case of unsuccessful selection to refuse object creation?

If before storage selection initialization of any objects they should be cleaned was led. Though, say that itself it will be cleaned , it is necessary to check up...
After that it is possible to throw an exception - in this case (throw from the designer) the object is not considered created ! And anything in addition it is not necessary to delete.

14

Re: How in the designer of object to refuse its creation?

I will study a question
Similar, it simply the general frightening on m any case of a word.

15

Re: How in the designer of object to refuse its creation?

MasterZiv wrote:

I will study a question

That was possible :
- Simply it is not caused .
- The heap, stack (object) - all it remains live after exception ejection.
- Members of a class and parent  are destroyed. I.e. parent  it is called.
Therefore after an exception it is necessary to clean everything that is selected. Therefore before burst it is better to clean all from within the designer.

16

Re: How in the designer of object to refuse its creation?

petrav wrote:

Qt basically does not work with exceptions. Their this decision. Your code should not throw out an exception in calls Qt. ., for example, you should not take off from the slot on an exception, should not take off on an exception of any overloaded method Qt etc.
There are details, it is possible to install the global handler of messages Qt and there to deliver the catch (...), but here I do not remember.

The global event handler does not interrupt a performance thread in which there was a problem. If the program one-continuous generally happens nothing before flow clearing. In my case there is the main flow servicing worker threads, but to hope that it especially quickly fulfills it is not necessary.
Where it is possible to read more in detail about all restrictions on usage of exceptions in Qt?

17

Re: How in the designer of object to refuse its creation?

Simply store all selected in smart pointers, and all will work as it is necessary.

18

Re: How in the designer of object to refuse its creation?

Now in exceptional situation I do such strange actions:

void MyLib:: SaveAndExit (const int i)
{
//error reports
if (i == 5) MyLib:: MessageError ("the Error: unsuccessful selection of storage!");
//here it is necessary to write the code of saving of the intermediate calculations
//global function invocation of end of operation
std:: terminate ();
}
void* MyLib:: AllocationMemory (const size_t Size)
{
void* Array = malloc (Size);
for (int i = 0; i <10 &&! Array; ++ i)
{
//we wait 1 second and we repeat attempt of selection of storage
QThread:: currentThread ()-> sleep (1);
Array = malloc (Size);
}
if (! Array)
{
try {int i = 5; throw i;}
catch (int i) {MyLib:: SaveAndExit (i);}
}
return Array;
}

It would be desirable instead of the unit:

try {int i = 5; throw i;}
catch (int i) {MyLib:: SaveAndExit (i);}

To write simply:

throw (int) 5;

And still a question: attempt to wait and try to select storage once again is how much pertinent?

19

Re: How in the designer of object to refuse its creation?

CEMb wrote:

that was possible :
- Simply it is not caused .

As though about it at once wrote:

Dima T wrote:

the Exception causes  already created objects

Object becomes created after successful end of operation of the designer. Therefore it:

CEMb wrote:

(object) - all it remains live after exception ejection.

cannot be true in any way. The object which has been never created remains live cannot.

CEMb wrote:

therefore after an exception it is necessary to clean everything that is selected. Therefore before burst it is better to clean all from within the designer.

it is too sweaty. It is better, as here already wrote, to use the classes, which behind itself clean. Clever pointers there, here it here all.

CEMb wrote:

that was possible :

On the future: there is such piece - the standard. Very much helps to clarify confidently as as should behave in language, without experimenting. In particular, see http://eel.is/c draft/except.ctor#3

20

Re: How in the designer of object to refuse its creation?

AlekseySQL wrote:

It would be desirable instead of the unit:

try {int i = 5; throw i;}
catch (int i) {MyLib:: SaveAndExit (i);}

To write simply:

throw (int) 5;

If I do not confuse, this unit is equivalent

MyLib:: SaveAndExit (5);
AlekseySQL wrote:

And still a question: attempt to wait and try to select storage once again is how much pertinent?

It is repeatedly possible to try if for wait time there will be a storage clearing.
If in parallel flows any storage is selected and it is expected that it will be soon released, that is sense to wait.

21

Re: How in the designer of object to refuse its creation?

Anatoly Moskovsky wrote:

Simply store all selected in smart pointers, and all will work as it is necessary.

Yes it is not necessary generally the smart pointer.
The array -  std:: vector this type is necessary.

22

Re: How in the designer of object to refuse its creation?

MasterZiv wrote:

Yes it is not necessary generally the smart pointer.
The array -  std:: vector this type is necessary.

It, type, not smart and not the pointer, aha.

23

Re: How in the designer of object to refuse its creation?

Dima T wrote:

If I do not confuse, this unit is equivalent
[code]
MyLib:: SaveAndExit (5);

Something I did not think of it smile

24

Re: How in the designer of object to refuse its creation?

AlekseySQL wrote:

Now in exceptional situation I do such strange actions:

Something here the tin unreal is written. Sentences/remarks:
1. To give to class MyClass an intelligent title.
2. If we are in a method of class Foo for a call of methods of this class it is not necessary to write permanently Foo:: myMethod ().
3. The codes of errors in int to replace on enum with speaking titles.
4. From terminate () to refuse.
5. Repeated attempts to select storage to delete.
6. From malloc () to refuse. To pass on new std:: uint8_t [Size] ().
7. From  to refuse especially.
8. It generally something improbable:

if (! Array)
{
try {int i = 5; throw i;}
catch (int i) {MyLib:: SaveAndExit (i);}
}

9. To try to pass on std:: vector, std:: vector:: resize and std:: vector:: reserve. But it will be difficult to you not to provoke copying of this object.
PS: But the most important thing, what you are going to do with the intermediate results of calculations? Here that? They in itself are of value or you for them launch any which other program of storage for some reason suffices?
PPS: What memory sizes in plan to manipulate?

25

Re: How in the designer of object to refuse its creation?

Dima T wrote:

it is passed...
It is repeatedly possible to try if for wait time there will be a storage clearing.
If in parallel flows any storage is selected and it is expected that it will be soon released, that is sense to wait.

, there is no sense to wait in such style as there is written. If we assume storage clearing in other flow to do it it is necessary somehow on another, is more intellectual (can condition variable).

Posts [ 1 to 25 of 28 ]

Pages 1 2 Next

You must login or register to post a reply