I assume this is a typo, that your B does not have public members at all, and that you lack public: before defining B::B(int,int) .
The author of the class represented by your A seems to want it to be cloned, but not copied. This suggests that he or she wants everything to live on the heap. But on the contrary, there the public constructor A::A(int) . Are you sure you are right about this?
It is plausible to assume that a class can reveal enough information about a given instance to constitute another instance. For example, a bit more meat on A :
class A { public: A(int a) : data_(a){}; std::shared_ptr<A> clone() const { return std::shared_ptr<A>(new A(*this)); } int data() const { return data_; } private: A(const A & a) {}; int data_; };
And if so, then the public constructor will make it just inconvenient to bypass the private, undefined copy constructor:
A a0(1); A a1{a0.data()};
Therefore, I am less sure that A correctly represents the class problem. However, given its meaning, the question you need to answer is: Can you even uncomfortably copy construction A ?
If not, then you're stuck. If so, then you can use the inconvenient copy of building A to explicitly define the normal copy constructor for B that you need. For example.
class B: public A { public: B(B const & other) : A(other.data()),extraData_(other.extraData_){} B(int data, int extraData): A(data), extraData_(extraData) { } std::shared_ptr<B> clone() const { return std::shared_ptr<B>(new B(*this)); } int extradata() const { return extraData_; } private: int extraData_; }; #include <iostream> int main() { B b(1,2); std::shared_ptr<B> pb = b.clone(); std::cout << pb->data() << std::endl; std::cout << pb->extradata() << std::endl; return 0; }
Mike kinghan
source share