C++ 尝试将对象传递给要复制的同一模板类
我实际上是为了理解模板的概念并实现一个简单的模板。我几乎成功地执行了一个实例来编写一个构造函数来复制同一个类的实例C++ 尝试将对象传递给要复制的同一模板类,c++,class,c++11,templates,C++,Class,C++11,Templates,我实际上是为了理解模板的概念并实现一个简单的模板。我几乎成功地执行了一个实例来编写一个构造函数来复制同一个类的实例 #include <iostream> // template <typename T> template <class T> class vec2 { private: static int instance; T const x; T const y; public: vec2() : x(0), y(0)
#include <iostream>
// template <typename T>
template <class T>
class vec2 {
private:
static int instance;
T const x;
T const y;
public:
vec2() : x(0), y(0) {
std::cout << "Default constructor" << std::endl;
vec2<T>::instance++;
return;
}
vec2(T const &x, T const &y) : x(x), y(y) {
std::cout << "Parametric constructor" << std::endl;
vec2<T>::instance++;
return;
}
vec2(vec2<T> const & src) {
*this = src;
std::cout << "Copy constructor" << std::endl;
vec2<T>::instance++;
return;
}
~vec2(){
std::cout << "Destructor" << std::endl;
vec2<T>::instance--;
return;
}
vec2 & operator=(vec2 const & rhs) {
this->x = rhs.get_x();
this->y = rhs.get_y();
return *this;
}
// get
static int get_instance() {
return vec2<T>::instance;
}
T get_x() const {
return this->x;
}
T get_y() const {
return this->y;
}
};
template <class T>
std::ostream & operator<<(std::ostream & out, vec2<T> const & rhs) {
out << "[ " << rhs.get_x() << ", " << rhs.get_y() << " ]";
return out;
}
template <class T>
int vec2<T>::instance = 0;
int main() {
vec2<float> a;
vec2<int> b(21, 42);
vec2<float> c(21.21f, 42.42f);
vec2<bool> d(true, false);
vec2<int> e(b);
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
std::cout << d << std::endl;
std::cout << e << std::endl;
std::cout << "a.get_x(): " << a.get_x() << std::endl;
std::cout << "a.get_y(): " << a.get_y() << std::endl;
std::cout << "b.get_x(): " << b.get_x() << std::endl;
std::cout << "b.get_y(): " << b.get_y() << std::endl;
std::cout << "c.get_x(): " << c.get_x() << std::endl;
std::cout << "c.get_y(): " << c.get_y() << std::endl;
std::cout << "d.get_x(): " << d.get_x() << std::endl;
std::cout << "d.get_y(): " << d.get_y() << std::endl;
return (0);
}
#包括
//模板
模板
第2类{
私人:
静态int实例;
T常数x;
T常数;
公众:
vec2():x(0),y(0){
std::cout正如@Sam Varshavchik在评论中所说的,问题在于您没有在副本构造函数中初始化const成员。以下是正确的实现:
vec2(vec2<T> const & src) : x(src.get_x()), y(src.get_y()) { //<-- initialization of const members
std::cout << "Copy constructor" << std::endl;
vec2<T>::instance++;
return;
}
vec2(vec2 const&src):x(src.get_x()),y(src.get_y()){/正如@Sam Varshavchik在评论中所说的,问题是您没有在副本构造函数中初始化const成员。以下是正确的实现:
vec2(vec2<T> const & src) : x(src.get_x()), y(src.get_y()) { //<-- initialization of const members
std::cout << "Copy constructor" << std::endl;
vec2<T>::instance++;
return;
}
vec2(vec2 const&src):x(src.get_x()),y(src.get_y()){/这里是我的原始类代码,我用*this=src
复制了一个副本,结果非常好。所以问题是,为什么它可以用常规类而不是模板来工作?
另外,我想象普通类的行为,地址保持不变。
如果类中有很多成员变量,初始化所有成员的方法可能会很复杂?
main.c
#include "Vec2.hpp"
#include <iostream>
int main() {
Vec2 a;
Vec2 b(21,42);
Vec2 c(a);
std::cout << a << std::endl;
std::cout << "c.get_x(): " << c.get_x() << std::endl;
std::cout << "c.get_y(): " << c.get_y() << std::endl;
std::cout << "b.get_x(): " << b.get_x() << std::endl;
std::cout << "b.get_y(): " << b.get_y() << std::endl;
std::cout << "a.get_x(): " << a.get_x() << std::endl;
std::cout << "a.get_y(): " << a.get_y() << std::endl;
a = b;
std::cout << "a.get_x(): " << a.get_x() << std::endl;
std::cout << "a.get_y(): " << a.get_y() << std::endl;
return 0;
}
vec2.hpp
#include "Vec2.hpp"
#include <iostream>
Vec2::Vec2() : x(0), y(0) {
std::cout << "Default constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::Vec2(float const x, float const y) : x(x), y(y) {
std::cout << "Parametric constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::Vec2(Vec2 const & src) {
*this = src;
std::cout << "Copy constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::~Vec2() {
std::cout << "Destructor" << std::endl;
Vec2::instance--;
return;
}
Vec2 & Vec2::operator=(Vec2 const & rhs) {
this->x = rhs.get_x();
this->y = rhs.get_y();
return *this;
}
int Vec2::get_instance(){
return Vec2::instance;
}
float Vec2::get_x() const {
return this->x;
}
float Vec2::get_y() const {
return this->y;
}
std::ostream & operator<< (std::ostream & out, Vec2 const & rhs) {
out << "[ " << rhs.get_x() << ", " << rhs.get_y() << " ]";
return out;
}
int Vec2::instance = 0;
#包括“Vec2.hpp”
#包括
Vec2::Vec2():x(0),y(0){
std::cout这里是我的原始类代码,我用*this=src
复制了一个副本,结果很好。所以问题是为什么这是常规类而不是模板的工作?
另外,我想象普通类的行为,地址保持不变。
如果类中有很多成员变量,初始化所有成员的方法可能会很复杂?
main.c
#include "Vec2.hpp"
#include <iostream>
int main() {
Vec2 a;
Vec2 b(21,42);
Vec2 c(a);
std::cout << a << std::endl;
std::cout << "c.get_x(): " << c.get_x() << std::endl;
std::cout << "c.get_y(): " << c.get_y() << std::endl;
std::cout << "b.get_x(): " << b.get_x() << std::endl;
std::cout << "b.get_y(): " << b.get_y() << std::endl;
std::cout << "a.get_x(): " << a.get_x() << std::endl;
std::cout << "a.get_y(): " << a.get_y() << std::endl;
a = b;
std::cout << "a.get_x(): " << a.get_x() << std::endl;
std::cout << "a.get_y(): " << a.get_y() << std::endl;
return 0;
}
vec2.hpp
#include "Vec2.hpp"
#include <iostream>
Vec2::Vec2() : x(0), y(0) {
std::cout << "Default constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::Vec2(float const x, float const y) : x(x), y(y) {
std::cout << "Parametric constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::Vec2(Vec2 const & src) {
*this = src;
std::cout << "Copy constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::~Vec2() {
std::cout << "Destructor" << std::endl;
Vec2::instance--;
return;
}
Vec2 & Vec2::operator=(Vec2 const & rhs) {
this->x = rhs.get_x();
this->y = rhs.get_y();
return *this;
}
int Vec2::get_instance(){
return Vec2::instance;
}
float Vec2::get_x() const {
return this->x;
}
float Vec2::get_y() const {
return this->y;
}
std::ostream & operator<< (std::ostream & out, Vec2 const & rhs) {
out << "[ " << rhs.get_x() << ", " << rhs.get_y() << " ]";
return out;
}
int Vec2::instance = 0;
#包括“Vec2.hpp”
#包括
Vec2::Vec2():x(0),y(0){
std::cout这与模板无关。如果您使用const
类成员声明一个常规类,并且您的构造函数未能初始化它,您将得到相同的错误。“*This=src;
”在初始化部分中必须明确构造X和Y。有关更多信息,请参见C++书籍。这与模板无关。如果用“代码> const 类成员声明正则类,而构造函数初始化失败,则会得到相同的错误。”<代码> *这= SRC;在初始化部分中,X和Y必须被明确地构造。更多信息,请参见您的C++书籍。问题不是因为规则和模板类。在这个实现中,您的<代码> x>代码>和<>代码> y>代码> MeMeBeS不是代码> const 。因此,*this=src
将调用(默认定义的)复制作业运算符,这就是为什么它可能会起作用,但不是你应该怎么做。这是所有这些信息。我使用这种方法是因为在42学校的tutovideo中,老师使用这种方法,但可能不是真正的最佳方法。thx适合你的灯光。问题不是因为常规类和模板类。在这个实现中,您的x
和y
成员不是const
。将它们设置为const
,您将得到相同的错误。此外,*this=src
将调用(默认定义的)复制作业运算符,这就是为什么它可能会起作用,但不是你应该怎么做。这是所有这些信息。我使用这种方法是因为在42学校的图托维迪奥,老师使用这种方法,但可能不是真正的最佳方法。thx适合你的灯光。