5

I'm trying to write some code that uses the "Coercion by Member Template" pattern in order to create a container that can hold pointers to multiple types and treat them polymorphically.

I've run into an issue trying to write a "coercion constructor?" (I'm not sure what you would really call it), but my code fails to compile stating that I can't access a private member of the class.

The regular copy constructor works fine and does have access, but for some reason using a different template type suddenly causes it to lose access.

#include <boost/shared_ptr.hpp>

template <typename T>
class Container {
public:
    Container<T>(T* contents) : m_contents(contents) {}

    // Copy constructor
    Container<T>(const Container<T>& other) : m_contents(other.m_contents) {}

    // Coercion constructor?
    template <typename U>
    Container<T>(const Container<U>& other) : m_contents(other.m_contents) {}

    T* get_contents() {
        return m_contents;
    }

private:
    T* m_contents;
};

// Things for the container to hold
struct A {};
struct B : public A {};

int main() {
    B* object1 = new B;
    Container<B> container1(object1);

    // Works fine
    Container<B> container2(container1);

    // Fails to compile because m_contents is private....
    Container<A> container3(container1);
}

How can I get access to the member variable in the "coercion constructor" without making it public? Making it public would break encapsulation and defeat the whole purpose of the container.

Also adding a "get_contents()" method and using it in the "coercion constructor" is not an option because I don't want the contents to be accessible from the outside.

tjwrona1992
  • 8,614
  • 8
  • 35
  • 98

0 Answers0