There is absolutely no casting required as
new
will return exactly the type you declared as return type. Maybe you are confusing this with
malloc()
, which returns a
void*
?
The only thing I can think of that you might want to change, is catching possible exceptions from the constructor -
if that is the purpose of your Create() function! You could for instance make a nothrow guarantee (see
http://www.gotw.ca/gotw/059.htm[
^]) like this:
XGGraphicAttribute* XGGraphicAttribute::Create() throw() {
XGGraphicAttribute* result = 0;
try {
result = new XGGraphicAttribute;
}
catch (std::bad_alloc) {
}
catch () {
}
return result;
}
Then again, it might be better to just let those exceptions hit the caller instead; that way there is no danger that the caller never thinks to test the result value - if the exception remains uncaught, it will immediately catch the attention of the developer, and (hopefully) lead to a fix.