Results 1 to 2 of 2
Hi All,
In C++ when I create an class object and when it calls the constructor.
If i do any dynamic allocation for a pointer variable and it creates an ...
- 12-01-2010 #1Just Joined!
- Join Date
- May 2009
- Posts
- 21
C++ handling objects
Hi All,
In C++ when I create an class object and when it calls the constructor.
If i do any dynamic allocation for a pointer variable and it creates an exception. How do i handle this exception inside the constructor and at the class object creation place.
My point is how do i handle exceptions inside the constructor and make my object creation success.
Thanks in advance,
Madhu
- 12-01-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Please post your code that is having the problem, along with the class definition. When you have to dynamically allocate memory in the construction phase of an object, there are some things you need to be careful with. As you have already seen, dealing with exceptions in a constructor like this can be a problem. You can have try/catch blocks inside the body of a constructor, but you cannot have them in the member initializer block which is outside of the body. The body is not entered properly until all base class constructors and member variable constructors have executed. So, if they throw an exception, you will have to catch it where you first invoke the construction of the object.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote