我如何触发父母类模板类的复制构造函数
如何在子类的复制构造函数中调用父模板类的复制构造函数?
// 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;
}
How can I call the copy constructor of the parent templated class inside the copy constructor of the child class?
// 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的基类是
parent&lt; t&gt;
此处。Your base class is
Parent<T>
here.