如何在子类中触发父模板类的复制构造函数

afp_2008

如何在子类的复制构造函数中调用父模板类的复制构造函数?

// Type your code here, or load an example.
#include<vector>
#include <iostream>

template <typename T>
class Parent {
public:
    Parent() {};
    const std::vector<T>& getDims() const { return m_dims; };
    void setDims(const std::vector<T> dims) { m_dims = dims; };
    Parent(const Parent& p) { m_dims = p.getDims(); };
private:
    std::vector<T> m_dims;
};

template <typename T>
class Child : public Parent<T> {
public:
    Child() {};
    const std::vector<T>& getCenter() const { return m_center; };
    void setCenter(const std::vector<T> center) { m_center = center; };
    Child(const Child& c) {
        m_center = c.getCenter();
        // How can I trigger the copy constructor of the parent class
        // and copy m_dims instead of the following
        this->setDims(c.getDims());
    }
private:
    std::vector<T> m_center;
};

int main(){
    Child<int> child1;
    child1.setDims({3, 1, 1, 1}); // Parent function
    child1.setCenter({1, 2, 3, 4}); // Child function

    Child<int> child2 = child1;
    return 0;
}
宁录
template <typename T>
class Child : public Parent<T> {
public:
    Child() {};
    Child(const Child& c): Parent<T>(c) {
        m_center = c.getCenter();
    }
private:
    std::vector<T> m_center;
};

你的基类在Parent<T>这里。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在子类构造函数中调用父构造函数?

如何在Codeigniter中将父类构造函数访问子类

我可以从Typescript的父类的构造函数中触发子类的成员函数吗?

如何在Swift子类中实现复制构造函数?

如何在父类的函数中调用子类的函数

如何在资源父类的构造函数中访问HttpServletRequest?

Java如何在调用父构造函数之前覆盖子类中的父类属性

如何在调用父构造函数之前覆盖子类中的父类属性?

如何在子类中将多个父类构造函数与val混合使用

Dart-如何在子类的构造函数中调用超类的工厂构造函数?

父/子类的C ++复制构造函数问题

从子类构造函数中访问父类方法

在父类和子类的构造函数中调用重写的方法

在python中从子类调用父类构造函数

在父类构造函数调用的方法中访问子类字段

模板类的复制构造函数

如何让子类中的构造函数继承构造函数或基类?

我们如何在C ++中从子类中调用父级重载的构造函数?

如何在子类构造方法中获取父类变量值

如何在模板派生类中调用模板基类的构造函数?

如何通过使用C ++中的构造函数获取父类的变量来在子类中实现Abstarct类的方法

如何在Flutter中从子类函数调用父类函数

嵌套类构造函数中的父模板参数推导

在 Moose 中,如何在子类中为超类指定构造函数参数?

C ++如何在容器类复制构造函数中复制分配器对象

当父类在 C++ 中没有默认构造函数时,如何在继承的类中使用构造函数?

如何在多态函数中传递父类实例代替子类实例?

如何在基类构造函数中为每个子类执行特定任务

如何在另一个类模板中定义完全专业的类的构造函数