CJ McAllister
CJ McAllister

Reputation: 331

Child of CObject in Copy Constructor Initializer List

I've been tasked with porting some legacy code, and in doing so I'd like to follow best practices as much as possible, since my predecessor regrettably did not. Below is a basic framework of the issue I've run into:

class Foo : public CPropertyPage
{
   // Constructor
   Foo() : CPropertyPage(Foo::ID)
   {
      pBar = new Bar();
      // init other things
   }

   // Copy Constructor
   Foo(const Foo& other) : CPropertPage(other) // C2248 compiler error
   {
      pBar = new Bar(*other.pBar);
      // copy other things
   }
}

Since I've read that it's best practice to write a copy constructor for a class when new is used in the constructor, that's what I'm trying to do. What I want to do, is include CPropertyPage's copy constructor in Foo's copy constructor initializer list, but this is not allowed, since CPropertyPage is a child of the MFC class CObject, whose copy constructor is private. If I try the constructor used in Foo's constructor initializer list, it compiles, but I'm not sure if this achieves the same end as CPropertyPage's copy constructor (Disclaimer: I'm quite new to copy constructors and initializer lists).

So, how can I properly write Foo's copy constructor? Is my approach fundamentally flawed in some way?

EDIT: Attempt at syntax fixes, identified CObject as MFC class

Upvotes: 0

Views: 224

Answers (1)

CJ McAllister
CJ McAllister

Reputation: 331

Below is an MSDN link pertaining to this issue:

http://msdn.microsoft.com/en-us/library/ccb3dh5c.aspx

Turns out my static analysis tool was being a little overzealous in checking for base classes in initializer lists.

Upvotes: 1

Related Questions