简体   繁体   English

用模板副本构造函数替换默认副本构造函数

[英]Replace the default copy constructor with a template copy constructor

I have a template class C. I want such a template copy constructor that it will resize the data array according to the size of the other class. 我有一个模板类C。我想要一个这样的模板副本构造函数,它将根据另一个类的大小来调整数据数组的大小。

The following is a simple example. 以下是一个简单的示例。 It works fine. 工作正常。 However, notice that Constructor 2 and Constructor 3 are so similar, I am wondering whether it is possible to merge them as one? 但是,请注意Constructor 2Constructor 3如此相似,我想知道是否可以将它们合并为一个?

Also, if I simple remove Constructor 3 , then C<int> c3( c1 ) won't call Constructor 2 , but will rather call a default copy constructor added by the compiler. 另外,如果我简单地删除了Constructor 3 ,那么C<int> c3( c1 )将不会调用Constructor 2 ,而是会调用由编译器添加的默认副本构造函数。 This will result in the memory not being allocated properly. 这将导致内存分配不正确。

template<typename T>
class C
{
public:
    T* val; 
    int size; 
public:

    C( int s = 0 ) {
        cout << "Constructor 1" << endl; 
        size = s; 
        val = ( size ) ? new T[size] : nullptr; 
    } 

    template<class T2>
    C( const C<T2>& c2 ) {
        cout << "Constructor 2" << endl; 
        size = c2.size; 
        val = ( size ) ? new T[size] : nullptr; 
    } 

    C( const C<T>& c2 ) {
        cout << "Constructor 3" << endl; 
        size = c2.size; 
        val = ( size ) ? new T[size] : nullptr; 
    } 

    ~C() {
        cout << "~C()" << endl; 
        delete[] val; 
        val = nullptr; 
    }
};

int main(void)
{
    C<int>   c1( 5 );
    C<float> c2( c1 );  
    C<int>   c3( c1 ); 

    return 0;
}

Output of the above function: 以上函数的输出:

Constructor 1
Constructor 2
Constructor 3
~C()
~C()
~C()

Replace 更换

template<class T2>
C( const C<T2>& c2 ) {
    cout << "Constructor 2" << endl; 
    size = c2.size; 
    val = ( size ) ? new T[size] : nullptr; 
} 

C( const C<T>& c2 ) {
    cout << "Constructor 3" << endl; 
    size = c2.size; 
    val = ( size ) ? new T[size] : nullptr; 
} 

by 通过

template<class T2>
C( const C<T2>& c2, int dummy) {
    cout << "Constructor 2" << endl; 
    size = c2.size; 
    val = ( size ) ? new T[size] : nullptr; 
}

template<class T2>
C( const C<T2>& c2 ) : C(c2, 0) {}

C( const C<T>& c2 ) : C(c2, 0) {} 

Update 更新

You can just use: 您可以使用:

C( int s = 0 ) {
    cout << "Constructor 1" << endl; 
    size = s; 
    val = ( size ) ? new T[size] : nullptr; 
} 

template<class T2>
C( const C<T2>& c2 ) : C(c2.size) {}

C( const C<T>& c2 ) : C(c2.size) {} 

and not need the second constructor. 不需要第二个构造函数。

One would like to implement this with a C++11 delegating constructor. 一个人想用C ++ 11委托构造函数来实现这一点。 However due to ambiguity in if template paramaters are to the class or to the constructor, this is not possible in a straightforward manner. 但是,由于模板参数对类还是对构造函数的含糊不清,因此不可能以直接的方式实现。 As a workaround one can factor out the common code into a helper function. 作为一种解决方法,可以将通用代码分解为辅助函数。

something like: 就像是:

    template<class T2>
    C( const C<T2>& c2 ) {
        Copy<T2>(c2);
    } 

    C( const C<T>& c2 ) {
        Copy<T>(c2);
    }

private:
    template<class T2>
    void Copy( const C<T2>& c2 ) {
        cout << "Constructor 2" << endl; 
        size = c2.size; 
        val = ( size ) ? new T[size] : nullptr; 
    }

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

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