清理C++构造函数中重复的代码

Fla*_*nix 0 c++ constructor

我在c ++中有一个头文件,为用户提供了几个构造函数(这是一个要求):

#ifndef IMANDEL_H_
#define IMANDEL_H_

class IMandel{

public:
    IMandel();
    IMandel(int aWidth, int aLength);
    IMandel(int threads, int aWidth, int aLength);
    //other stuff!
private:
    int max_iterations, thread_count, height, width;
    int* buffer; 
};

#endif
Run Code Online (Sandbox Code Playgroud)

因此,在我对应的cpp文件中,我分别实现了这些构造函数:

//default constructor
IMandel::IMandel(){
    height = 10000;
    width = 10000;

    //this code segements gets repeated in every constructor! Messy!
    max_iterations = 255;
    thread_count = 1;
    buffer = new int[width*height];
}

IMandel::IMandel(int aWidth, int aLength){
    width = aWidth;
    height = aLength;

     //this code segements gets repeated in every constructor! Messy!
    max_iterations = 255;
    thread_count = 1;
    buffer = new int[width*height];
}

IMandel::IMandel(int threads, int aWidth, int aLength){
    thread_count = threads;
    width = aWidth;
    height = aLength;

     //this code segements gets repeated in every constructor! Messy!
    max_iterations = 255;
    buffer = new int[width*height];
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我的构造函数不健康,他们在各处都重复了大量的代码,这太可怕了!

在java中,我通过使用构造函数相互调用找到了解决此问题的方法.基本上我重用了以下构造函数(Java示例):

public myClass(){
    this(1, 10000, 10000);
}

public myClass(int aWidth, int aLength){
   this(1, aWidth, aLentgh);
}

public myClass(int threads, int aWidth, int aLength){
   thread_count = threads;
   width = aWidth;
   height = aLength;
   max_iterations = 255;
   buffer = new int[width*height];
}
Run Code Online (Sandbox Code Playgroud)

正如您在此Java示例中看到的那样,各种构造函数之间没有重复的代码.问题:

  1. 有没有办法在C++中实现同样的效果?
  2. 如果是这样的话?你能提供样品吗?
  3. 如果没有,您建议使用什么解决方案来解决问题?

Dre*_*ann 7

实际解决方案会有所不同,具体取决于您使用的C++版本.

在C++ 03中,一个常见的(但不完美的 - 在底部看到有用的注释)方法是创建一个init()所有构造函数调用的函数.你的所有三个构造函数都可以是一行调用这样的函数:

void IMandel::init(int threads, int aWidth, int aLength){
    thread_count = threads;
    width = aWidth;
    height = aLength;

     //this code segements gets repeated in every constructor! Messy!
    max_iterations = 255;
    buffer = new int[width*height];
}

//default constructor
IMandel::IMandel(){
    init( 1, 10000, 10000 );
}

IMandel::IMandel(int aWidth, int aLength){
    init( 1, aWidth, aLength );
}

IMandel::IMandel(int threads, int aWidth, int aLength){
    init( threads, aWidth, aLength );
}
Run Code Online (Sandbox Code Playgroud)

在C++ 11中,允许构造函数调用其他构造函数,正如@chris所评论的那样.您可以通过以下方式更改构造函数:

//default constructor
IMandel::IMandel()
: IMandel( 1, 10000, 10000 )
{
}

IMandel::IMandel(int aWidth, int aLength)
: IMandel( 1, aWidth, aLength )
{
}

IMandel::IMandel(int threads, int aWidth, int aLength){
    thread_count = threads;
    width = aWidth;
    height = aLength;

     //this code segements gets repeated in every constructor! Messy!
    max_iterations = 255;
    buffer = new int[width*height];
}
Run Code Online (Sandbox Code Playgroud)

  • `init`函数解决方案不支持不能默认构造的数据成员,或者默认构造成本高的数据成员. (3认同)