Copying a C++ class with a member variable of reference type

后端 未结 7 2348
清酒与你
清酒与你 2020-12-30 06:06

I\'ve a class which stores a reference to its parent, the reference is passed in the constructor. If I try to copy an instance I get an error \"error C2582: \'operator =\' f

相关标签:
7条回答
  • 2020-12-30 06:50

    Yes just make the member a pointer. A reference won't be able to be reseated, and there is no work-around.

    Edit: @"Steve Jessop" makes a valid point to how work-around the problem using the PIMPL idiom (private implementation using a "d-pointer"). In an assignment, you will delete the old implementation and create a new one copy-constructed from the source object's d-pointer.

    0 讨论(0)
提交回复
热议问题