Class 头文件中的声明和实现
Class declaration and implementation in the header file
我在大学有作业。我的任务是完成两个头文件并实现给定的 类。在那些头文件中实现很重要,因为它会通过单元测试进行测试,并且只会测试头文件。
假设我有这个 .h 文件:
#pragma once
template<class T>
class RareVector;
template<class T>
class Vector
{
public:
Vector(){}
Vector(int dim);
Vector(T *t, int dim);
Vector(const Vector&);
~ Vector();
Vector operator+(const Vector&);
Vector operator-(const Vector&);
double operator*(const Vector&);
double operator~();
double operator%(const Vector&);
T operator[](int) const;
operator RareVector<T>();
private:
T* m_t;
int m_dim;
};
我的问题是: 实现这些 类 的唯一地方是在声明中,或者我可以在下面的某个地方做这样的事情:
template <class T>
Vector<T>::Vector(){
// code goes here
}
您可以使用内联来实现声明之外的功能...
template<class T>
class Vector
{
public:
Vector(){}
Vector(int dim);
inline Vector(T *t, int dim);
Vector(const Vector&);
~ Vector();
Vector operator+(const Vector&);
...
};
template<class T>
inline Vector::Vector(T *t, int dim)
{
}
我在大学有作业。我的任务是完成两个头文件并实现给定的 类。在那些头文件中实现很重要,因为它会通过单元测试进行测试,并且只会测试头文件。
假设我有这个 .h 文件:
#pragma once
template<class T>
class RareVector;
template<class T>
class Vector
{
public:
Vector(){}
Vector(int dim);
Vector(T *t, int dim);
Vector(const Vector&);
~ Vector();
Vector operator+(const Vector&);
Vector operator-(const Vector&);
double operator*(const Vector&);
double operator~();
double operator%(const Vector&);
T operator[](int) const;
operator RareVector<T>();
private:
T* m_t;
int m_dim;
};
我的问题是: 实现这些 类 的唯一地方是在声明中,或者我可以在下面的某个地方做这样的事情:
template <class T>
Vector<T>::Vector(){
// code goes here
}
您可以使用内联来实现声明之外的功能...
template<class T>
class Vector
{
public:
Vector(){}
Vector(int dim);
inline Vector(T *t, int dim);
Vector(const Vector&);
~ Vector();
Vector operator+(const Vector&);
...
};
template<class T>
inline Vector::Vector(T *t, int dim)
{
}