Classic/Standard. However, I do hate having to use pointer semantics for all of my operations. So I thought: If I never ever manipulate the actual pointer itself, why not just keep a reference?
Code:
class MyClassImpl;
class MyClass
{
public:
MyClass() : impl(*new MyClassImpl) {}
~MyClass(){delete &impl;}
private:
MyClassImpl& impl;
};
Everything seems legit to me...
The only downside that I see, is that I can't swap or move, but this particular object is not meant to be swapped or moved.
Did I miss anything here, or can I go ahead and do this.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
You wouldn't be able to store it in a container either.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
BTW, probably it was just for brevity, but the code snippet is wrong because you're new-ing an incomplete type ( this is an error ) and you're deleting an incomplete type ( this is UB, in general ).
as far as the reference is concerned, the only problem I see is in the event of "new" returning 0; anyway, nothing really serious ...
BTW, probably it was just for brevity, but the code snippet is wrong because you're new-ing an incomplete type ( this is an error ) and you're deleting an incomplete type ( this is UB, in general ).
Yeah, you caught that. It was for brevity. The code is actually in a cpp.
Originally Posted by superbonzo
as far as the reference is concerned, the only problem I see is in the event of "new" returning 0; anyway, nothing really serious ...
Originally Posted by laserlight
Doesn't sound like a problem though since nothrow new was not used.
Yup.
Thanks for your feedbacks.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Bookmarks