|
-
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.
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
|