C ++,为什么我不能在类声明之外使用参数初始化列表来定义类构造函数

时间:2016-11-04 02:47:07

标签: c++ class constructor declaration

为什么我不能在类声明之外使用参数初始化列表来定义类构造函数?

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();

1 个答案:

答案 0 :(得分:1)

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()
{
}