cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Member Data on the Heap
Member Data on the Heap
Feb 5, 2014 at 12:56am UTC
geniusberry
(83)
Hello!
Now straight to business. I was wondering, if you had class member data on the heap, like for example
int
*pfoo =
new
int
;
how would you handle it being null?
you're checking for a null pointer in the constructor,
1
2
3
4
if
(pfoo == NULL) {
delete
pfoo; pfoo = 0; }
and if it is null, would the program stop automatically, and halt constructing the object, or is there a way to deal with this if it happens?
Feb 5, 2014 at 1:10am UTC
Peter87
(11234)
If
new
int
fails it will throw an exception of type std::bad_alloc. If this happens inside the constructor and the exception is not caught this will prevent the object from being created.
Feb 5, 2014 at 12:06pm UTC
geniusberry
(83)
thanks!
Topic archived. No new replies allowed.