简体   繁体   English

c ++是参数化构造函数中调用的默认构造函数吗?

[英]c++ is default constructor called in parametrized constructor?

I have the following template class: 我有以下模板类:

template<typename T, int nSize> class Stack{
private:
    int m_nCurrentPos;
    Array<T> m_tArray;
public:
    Stack(int nCurrentPos = 0);
    ...
};

I would like the default constructor work as follows: 我想默认构造函数的工作方式如下:

template<typename T, int nSize> Stack<T,nSize>::Stack(int nCurrent){ 
    m_nCurrent = nCurrentPos;
    m_tArray = Array<T>::Array(nSize);
};

Where Array looks like that: Array看起来像这样:

template <typename T> class Array{
private:
    T* m_cData;
    int m_nSize;
    static int s_nDefaultSize;
public:
    Array();
    Array(int nSize);
    ...
};

And its constructors are: 它的构造函数是:

template<typename T> Array<T>::Array(){
    m_nSize = s_nDefaultSize;
    m_cData = new T[m_nSize];
}

and, obviously, 显然,

template<typename T> Array<T>::Array(int nSize){
    m_nSize = nSize;
    m_cData = new T[m_nSize];
}

So, Stack is composed from Array and some extra functionalities. 因此,Stack由Array和一些额外的功能组成。 My problem is that when in Array I define s_nDefaultSize to be say 512 , and I try to instantiate Stack<int,1024> I'm getting an exception from class Array, saying that I'm trying to assign two arrays of different length. 我的问题是 ,在数组中,我将s_nDefaultSize定义为512 ,我尝试实例化Stack<int,1024>我从类Array中得到一个异常,说我正在尝试分配两个不同长度的数组。 As soon as I change the code such that s_nDefaultSize=1024 (so it matches the template's non-type argument nSize ), everything's fine. 一旦我更改代码使得s_nDefaultSize=1024 (因此它匹配模板的非类型参数nSize ),一切都很好。 So, in other words, the exception occurs as soon as s_nDefaultSize != nSize . 换句话说,只要s_nDefaultSize != nSize就会发生异常。 So, my guess is that in the code above, in the default constructor of Stack<T,nSize> , ie, m_tArray = Array<T>::Array(nSize); 所以,我的猜测是在上面的代码中,在Stack<T,nSize>的默认构造函数中,即m_tArray = Array<T>::Array(nSize); the m_tArray is created by default constructor of Array (employing s_nDefaultSize ) and only then the compiler tries to assign it to Array<T>::Array(nSize) (employing my nSize value). m_tArray是由Array的默认构造函数(使用s_nDefaultSize )创建的,然后编译器才会尝试将其分配给Array<T>::Array(nSize) (使用我的nSize值)。 Is that correct? 那是对的吗? And if yes, how can I change this behaviour? 如果是,我该如何改变这种行为? This is another question that I posted yesterday, which, even though is about inheritance, not composition, is very much related to the question in this thread. 这是我昨天发布的另一个问题,即使是关于继承,也不是组合,与这个帖子中的问题非常相关。 Cheers! 干杯!

You need to use a member initialization list with the constructor of Stack : 您需要使用成员初始化列表和Stack的构造函数:

template<typename T, int nSize>
Stack<T,nSize>::Stack(int nCurrent)
  : m_tArray(nSize)
{ 
    m_nCurrent = nCurrentPos;
};

As you have it, m_tArray will first be default constructed and then assigned to in your constructor. 如你m_tArraym_tArray将首先默认构造,然后在构造函数中分配。 Here, we use the member initialization list to initialize m_tArray to the right size immediately. 在这里,我们使用成员初始化列表立即将m_tArray初始化为正确的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM