0

If I have a struct that consists of two objects of a class, and I create an array of those structs without initially setting the values of the objects to anything, what will be contained in that array? Does it create structs using the default constructor for that struct?

My specific example requires me to use the last element of the array recursively to determine what the next element of the array is, and I need all the elements initially set to what the default constructor provides.

The array I am creating and the way I am declaring it is:

s arrayname [5];

My struct is essentially along the lines of:

struct s{
    Vectorh a, b;

    s();
}

where Vectorh is a class that contains four double values, plus a lot of functions. I also have a default constructor in this struct, which is defined as:

s(){
    a = Vectorh();             // Vectorh() creates a Vectorh with all components set
    b = Vectorh();             // to (double)0
}

Finally, Vectorh is defined as:

class Vectorh{
    double x, y, z, w;
}

with the constructors

Vectorh(double a, double b, double c){
    x = a;
    y = b;
    z = c;
    w = (double)0;
}
Vectorh(){
    x = (double)0;
    y = (double)0;
    z = (double)0;
    w = (double)0;
}

Edit: The linked question says nothing about empty initialization at all. It only talks about initialization with elements.

9
  • Yes, the default constructor will be used. Commented Nov 20, 2016 at 0:28
  • Use ... ={}; to value initialise the array. Default is uninitialised. Commented Nov 20, 2016 at 0:28
  • It depends on the "struct". This can't be answered reasonably without more information. Commented Nov 20, 2016 at 0:31
  • @juanchopanza I added some information. Commented Nov 20, 2016 at 0:36
  • You need to include a complete but minimal example that readers can try as-is. For now voting to close as lacking such example. Commented Nov 20, 2016 at 0:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.