Thread: Can I call the NULL constructor from another constructor to save code duplication?

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    7

    Can I call the NULL constructor from another constructor to save code duplication?

    The code below works. I have a simple class with one integer and an array of integers. I have a NULL constructor that initializes the integer to 0, and initializes the array to have 0 elements. I have a second constructor that initializes the integer to the parameter passed to it and initializes the array to have 0 elements. This trivial example has 2 lines of duplicated constructor code for the array, but it's easy to imagine much more duplication. I am wondering if it's possible to only have the array initialization code in the NULL constructor and have one constructor call the other constructor so everything gets initialized without duplicating code.

    Code:
    #include <iostream>
    using namespace std;
    
    
    
    
    class a
    {
    private:
        int x;
        int array_size;
        int* array;
    public:
        a();
        a(int param_x);
        ~a();
    
    
    friend ostream& operator << (ostream& outs, const a& obj);
    };
    
    
    
    
    a::a()
    {
        cout << "NULL Constructor\n";
        x = 0;
        array_size = 0;
        array = NULL;
    }
    
    
    
    
    a::a(int param_x)
    {
        cout << "int Constructor\n";
    
    
        // Can I replace the two lines below with a call to the NULL constructor?
        array_size = 0;
        array = NULL;
    
    
        x = param_x;
    }
    
    
    
    
    a::~a()
    {
        cout << "Destructor\n";
    
    
        if(array != NULL)
        {
            delete []array;
        }
    }
    
    
    
    
    ostream& operator << (ostream& outs, const a& obj)
    {
        outs << "x=" << obj.x << ":array_size=" << obj.array_size << endl;
        if(obj.array == NULL)
        {
            return outs;
        }
        for(int i = 0; i < obj.array_size; i++)
        {
            outs << "array[" << i << "]=" << obj.array[i] << "\n";
        }
        return outs;
    }
    
    
    
    
    int main()
    {
        a object(8);
        cout << object;
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The term you are using (NULL constructor) is wrong .... and misleading.

    The recent C++ standard (C++-11) allows delegation between constructors. However, depending on age of your compiler, that may not be supported.

    However, in your case, it is not necessary. Eliminate the constructor with no arguments, and specify a default value for the int constructor. I've modified your code to show the idea (and removed statements doing I/O). Note, in particular, the parts I have highlighted in red.
    Code:
    class a
    {
    private:
        int x;
        int array_size;
        int* array;
    public:
    
        a(int param_x = 0);    //  Note default value
        ~a();
     
     
    friend ostream& operator << (ostream& outs, const a& obj);
    };
    
    a::a(int param_x)     //  Note:  do not specify default value here
    {
        array_size = 0;
        array = NULL;
        x = param_x;
    }
    A default value for a function (in this case constructor) argument simply means the compiler supplies that value, if the programmer does not.

    I'd probably also declare the constructor to be explicit, but that's going beyond the bounds of your question.

    Also, the destructor does not need to test that array is NULL. delete NULL is guaranteed by the standard to do nothing.
    Last edited by grumpy; 06-23-2012 at 10:31 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Makes sense and worked like a charm. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  2. Replies: 3
    Last Post: 11-16-2009, 07:51 AM
  3. Replies: 10
    Last Post: 06-02-2008, 08:09 AM
  4. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  5. Call constructor within constructor. How to?
    By xErath in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 05:30 PM