抽象模板类和继承构造函数

Abstract template class and inheritance contructor

本文关键字:继承 构造函数 抽象      更新时间:2023-10-16

我有构造函数的问题,我想做这样的事情:

A<int>* first = new B<int>
A<int> *second;
second= new B<int>(*A);

我已经尝试在参数列表中使用指针,引用和值,没有任何工作,我不知道为什么。这是我的不工作的构造函数:

template <class T>
B<T>::B(B<T> other) 

它与const A<T>&工作,谢谢,还有一件事,如果可以的话。我必须在这个构造函数中访问B类私有类字段。如果只有A类。我能改天再做吗?

假设B<T>继承了A<T>,正如您在评论中所描述的,当您编写new B<int>( some_value_of_type_A );

时,您正在尝试向下转换。

必须提供一个A<T>类型的构造函数,如下所示:

template<class T>
B<T>::B(const A<T>& other);