|
-
June 18th, 2012, 06:03 AM
#1
reference semantic for pimpl class
I have a basic class with a pimpl, with agressive instantiation:
Code:
class MyClassImpl;
class MyClass
{
public:
MyClass() : pImpl(new MyClassImpl) {}
~MyClass(){delete pImpl;}
private:
MyClass(const MyClass&) = delete;
MyClass& operator=(const MyClass&) = delete;
MyClassImpl* pImpl;
};
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 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.
-
June 18th, 2012, 08:47 AM
#2
Re: reference semantic for pimpl class
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
-
June 18th, 2012, 12:37 PM
#3
Re: reference semantic for pimpl class
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 ...
-
June 18th, 2012, 12:43 PM
#4
Re: reference semantic for pimpl class
 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 ...
Doesn't sound like a problem though since nothrow new was not used.
-
June 18th, 2012, 02:00 PM
#5
Re: reference semantic for pimpl class
 Originally Posted by superbonzo
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 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|