Sponsored Content
Top Forums Programming how do you handle a constructor and destructor that fail Post 302132376 by porter on Thursday 16th of August 2007 05:16:56 PM
Old 08-16-2007
Quote:
Originally Posted by amitpansuria
can u explain me step by step why exception is not thrown from the destructor that fails with an example
Regards,
Amit
No, I'm saying that you shouldn't have code in your destructor that can fail.

If you can't achieve that then you handle then use try/catch in your destructor around the part that may fail.
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Constructor problem

Hi guys I am new to these forums but since I am taking a class at college I would appreciate any help that is possible for this program. My instructor said that when its complete the program should be able to store all 3 fields instead of just 1. public class Greeter2Test { public static... (4 Replies)
Discussion started by: woot4moo
4 Replies

2. Programming

why constructor cannot be virtual

helo i read many books but i cant find the proper answer that why constructor cannot be virtual can u explain me in simple term that why constructor cannot be virtual Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

3. Shell Programming and Scripting

call constructor of java class in script

Hi, Is it possible to call the constructur of a java class in a shell script? I know you can call static methods, but can you also call the constructor? tnx. (1 Reply)
Discussion started by: thebladerunner
1 Replies

4. Programming

Doubt regarding Copy Constructor and return value

Hi All, I have made the simple following program :- #include <string> #include <iostream> using namespace std; class A{ private: int val; public : A(){cout<<"In A()"<<endl;} A (const A& aa) { cout<<"In copy c'tor"<<endl; } }; A f(... (1 Reply)
Discussion started by: shubhranshu
1 Replies

5. Programming

c++ object constructor question

I have the following code class Param{ public: Param(int aa, int bb){ a=aa; b=bb; } int a,b; }; void function(Param); int main(){ function(2,3); return 0; } (6 Replies)
Discussion started by: santiagorf
6 Replies

6. Programming

Doubts on C++ copy constructor concept

Hi, If I run the following program class A { public: A() { cout << "default" << endl; } A(const A&) { cout << "copy" << endl; } }; A tmp; A fun() { return tmp; } A test() { A tmp; cout << &tmp << endl; return tmp; } (1 Reply)
Discussion started by: royalibrahim
1 Replies

7. UNIX for Dummies Questions & Answers

Difference between handle to the thread HANDLE and thread identifier pthread_t

This question might be silly but its confusing me a bit: What is the difference between handle to the thread HANDLE and thread identifier pthread_t? ---------- Post updated at 01:52 PM ---------- Previous update was at 01:48 PM ---------- Sorry I saw details and HANDLE is in windows and... (0 Replies)
Discussion started by: rupeshkp728
0 Replies

8. Programming

Constructor?

I am learning about C++ and today am reading concepts for Constructor but it seems a bit difficult to grab it fully. Please anyone explain in simple words about Constructor? (1 Reply)
Discussion started by: ggiwebsinfo
1 Replies
libtalloc_destructors(3)					      talloc						  libtalloc_destructors(3)

NAME
libtalloc_destructors - Chapter 4: Using destructors Using destructors Destructors are well known methods in the world of object oriented programming. A destructor is a method of an object that is automatically run when the object is destroyed. It is usually used to return resources taken by the object back to the system (e.g. closing file descriptors, terminating connection to a database, deallocating memory). With talloc we can take the advantage of destructors even in C. We can easily attach our own destructor to a talloc context. When the context is freed, the destructor will run automatically. To attach/detach a destructor to a talloc context use: talloc_set_destructor(). Example Imagine that we have a dynamically created linked list. Before we deallocate an element of the list, we need to make sure that we have successfully removed it from the list. Normally, this would be done by two commands in the exact order: remove it from the list and then free the element. With talloc, we can do this at once by setting a destructor on the element which will remove it from the list and talloc_free() will do the rest. The destructor would be: int list_remove(void *ctx) { struct list_el *el = NULL; el = talloc_get_type_abort(ctx, struct list_el); /* remove element from the list */ } GCC version 3 and newer can check for the types during the compilation. So if it is our major compiler, we can use a more advanced destructor: int list_remove(struct list_el *el) { /* remove element from the list */ } Now we will assign the destructor to the list element. We can do this directly in the function that inserts it. struct list_el* list_insert(TALLOC_CTX *mem_ctx, struct list_el *where, void *ptr) { struct list_el *el = talloc(mem_ctx, struct list_el); el->data = ptr; /* insert into list */ talloc_set_destructor(el, list_remove); return el; } Because talloc is a hierarchical memory allocator, we can go a step further and free the data with the element as well: struct list_el* list_insert_free(TALLOC_CTX *mem_ctx, struct list_el *where, void *ptr) { struct list_el *el = NULL; el = list_insert(mem_ctx, where, ptr); talloc_steal(el, ptr); return el; } Version 2.0 Tue Jun 17 2014 libtalloc_destructors(3)
All times are GMT -4. The time now is 03:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy