CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,273

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

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,670

    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

  3. #3
    Join Date
    Oct 2008
    Posts
    1,004

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

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,033

    Re: reference semantic for pimpl class

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,273

    Re: reference semantic for pimpl class

    Quote Originally Posted by superbonzo View Post
    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.

    Quote Originally Posted by superbonzo View Post
    as far as the reference is concerned, the only problem I see is in the event of "new" returning 0; anyway, nothing really serious ...
    Quote Originally Posted by laserlight View Post
    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.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width