We help IT Professionals succeed at work.

C++ Object Construction Check

Pra Sys
Pra Sys asked
on
Medium Priority
519 Views
Last Modified: 2012-08-13
Apart from catching exceptions thrown from within constructor, are there any other efficient, straight and legitimate ways to check if construction of object is completed successfully in C++?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi mumbaikar,

IMO there's no other method - an object is constructed completely as soon as the used constructor returns.

Look at this interesting article about some detailed info about lifetime of objects: http://www.gotw.ca/gotw/066.htm

ZOPPO
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
Just to confirm what Zoppo has stated, there is only one definition of "failed to construct" and that is if the constructor throws. In all other circumstances the object will be created. This is specifically true because in all cases other than when the constructor percolates an exception the destructor WILL be called.

Of course, that doesn't mean it will be legitimate to use from a business logic point of view but from a C++ point of view the object will be legitimate.
Pra SysSoftware Professional

Author

Commented:
Thanks Zoppo and evilrix. Please comment if my understanding below is correct -

1. If we allocate memory using new in member intializer list and there is an exception happens in construction body, then we have no way to de-allocate that memory.

2. If we allocate memory using new in constructor body and there is an exception happens in constructor body, then also we have no way to de-allocate any memory

Since construction is not complete, destructor will not be called in this case. I have written a sample code below to elaborate my question. None of the catch block in this code is executed. The program crashes. Do you think this behaviour is correct? Can this be improved to stop memory leaks? I have added line "strcpy(str,ptr)" to facilitate exception.

class Base {
public:
	Base() try :ptr(0), str(0)
		 {
			ptr = new char[8];
			strcpy(ptr,"Prasad");

			strcpy(str,ptr);
		}
		catch (...) {
			delete [] ptr;
			cout << "catching in Base constructor..." << endl;
		}


	char * getptr() {
		return ptr;
	}

	~Base() {
		delete [] ptr;
	}
private:
	char *ptr;
	char *str;
};

int main() {
	try {
		Base b;
		cout << b.getptr () << endl;
	}
	catch (...) {
		cout << "catching in Main function..." << endl;
		throw "Hello";
	}

	return 0;
}

Open in new window

Pra SysSoftware Professional

Author

Commented:
Please ignore <<throw "Hello";>> on line 35. Thanks.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Senior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Pra SysSoftware Professional

Author

Commented:
Thanks for help guys.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.