C++,为什么我不能把带有参数的类构造函数的定义-初始化列表放在类声明之外

C++, Why can't I put the definition of class constructor with parameters-initialize list outside the class declaration

本文关键字:列表 初始化 声明 定义 构造函数 不能 C++ 参数 为什么      更新时间:2023-10-16

为什么不能将类构造函数的定义与参数初始化列表放在类声明之外?

typedef unsigned int UINT;
class num_sequence {
public:
    typedef vector<UINT>::iterator iterator;
    //I cannot put this following part in the cpp file
    num_sequence(vector<UINT> & ele,int len=0,int beg=0):_relems(ele),_length(len),_beg_pos(beg)
    {
        //an abstract class cannot be instanlized
        cout<<"build a num_sequence object";  
    }    
    virtual ~num_sequence();

num_sequence.h

#include <vector>
typedef unsigned int UINT;
class num_sequence
{
public:
    typedef std::vector<UINT>::iterator iterator;
    num_sequence(std::vector<UINT> & ele, int len = 0, int beg = 0);
    virtual ~num_sequence();
private:
    std::vector<UINT> &_relems;
    int _length;
    int _beg_pos;
};

num_sequence.cpp

#include "num_sequence.h"
#include <iostream>
num_sequence::num_sequence(std::vector<UINT> & ele, int len, int beg)
    : _relems(ele), _length(len), _beg_pos(beg)
{
    std::cout << "build a num_sequence object";  
}    
num_sequence::~num_sequence()
{
}