1

Define an array of strings and initialize every other element to "widget". Elements not initialized to "widget" should be initialized by the default constructor.

How would I do this? The only way to initialize an array is with the default constructor. I was thinking about doing something like this, but wasn't sure if this would be valid:

string strings[3] = {string(), string ("widget"), string()};

1
  • 2
    It's valid, except you need to add the extra } before ;. Commented Oct 26, 2014 at 5:21

3 Answers 3

1

When you do:

string strings[3] = {string(), string ("widget"), string()};

You are specifying the array size explicitly (could have been implicit from the contents), as content you are providing individual string objects causing the initilaization of the contents done by calling copy constructors again (unnecessary although correct), so you could do as well as:

string strings[] = {"", "widget", ""};
Sign up to request clarification or add additional context in comments.

Comments

0

You can do

string strings[] = {"", "widget", ""};

The compiler will figure out the rest

Comments

0
string strings[] = {{}, {"widget"}, {}, {"widget"}};

Comments

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.