views:

305

answers:

4

Hi all, the code below gives compilation error when I try to create test t[2]; because there is no default constructor for this.

But if I create Test t[2] = {test(1,2), test(2,3)}; Then it works fine.

1)But think of a situation, if we want to create more then 100 array element. We need to create 100 element in the curly braces like.. Test t[100] = {test(1,2), test(1,2)……/100 times/};

The above code is difficult to maintain. One more solution is to create public member function which takes 2 integers and run in a loop. This solves the problem but i want to know any other good method.

2) If I create it using new

Test *t = new test[10];

I get compilation error(No default constructor). How to solve this.

class test
{
    int _a;int _b;

public:
    test(int a, int b);
    void display();
};


int _tmain(int argc, _TCHAR* argv[])
{
    test t[10];

    for (int i = 0 ; i< 10; i++)
        t[i].display();
}
+1  A: 

In your example what do you expect to be displayed?
If you know that, you can write a Default CTor (one that has no parameters) and set your values to the defaults.

An example of the Default CTor:

// Variant 1: Use the initialization list
test()
: a(-1)
, b(-1)
{
}

// OR
// Variant 2: Do it in the CTor's body
test()
{
    a = -1;
    b = -1;
}

Note: You can write several CTors (it's called "overloading"). One that takes no parameters and sets default values and others that take parameters and set those values.

mxp
display function is like thiscout<<"a = "<<_a<<"b ="<<_b;if i write a default constructor or one that has no parameters. then how can i pass values in runtime to the variables.
Shadow
If you don't pass values at construction time, you must pass them later, for example (1) setValues(int a, int b) or (2) setA(int a) and setB(int b) or (3) directly modify the member variables.
mxp
@GrabIt: Add some accessor methods setA(), getA() to your class to change a and b later on. To paraphrase mxp's question: is it even sensible to implicitly set a and b to -1 (or zero, or some other value) in a default constructor (which IS just exactly the constructor with no parameters)?
digitalarbeiter
+8  A: 

This FAQ answers exactly this question.

Naveen
+2  A: 

In order to construct your 10 elements in the array the compiler somehow has to instaciate them through a constructor. For arrays only a default constructor (taking no arguments) can bes used, as you can not pass any arguments to the elements in the array. Therfor you have to proved a constructor

test::test()

taking no arguments.

RED SOFT ADAIR
is array can be created only through the default constructor?is it not possible to create array with other constructor?
Shadow
No, that's not possible. How would you write that down? There is no syntax for this.
RED SOFT ADAIR
Thanks,Test t[2] = {test(1,2), test(2,3)}; ..we can write this way, but if the number of elements increases, its hard to maintain.. so i am asking any other way is there?
Shadow
Yes, you can call a individual constructor for each element, but its not possible to call a parameterized constructor for all elements together. There are only these tweo possibilities.
RED SOFT ADAIR
A: 

You can also define a constructor with default values for all parameters which will be used as the default constructor.

test(int a = 0, int b = 0) :_a(a), _b(b) {}

Since all parameters have default values, this constructor will be used as the default. Leaving out the initialization list or not initializing the member variables in the body of the constructor may give you random data values. Some systems may zero all memory allocations, but some do not.

Dave Delaney