0

I declared an array of size 100 string bin[100]; I want each array to be initialized as "". How do I initialize all of them with one line? I tried something like bin[] = ""; and it doesn't work. Any help would be appreciated.

Default Constructor

RandomString()
{
    input = "";
    //bin[] = "";
}

2 Answers 2

2

You don't have to do anything special, just string bin[100]; is fine. The default constructor for std::string will be called for each element of the array.

If the array is a member of some class, initialize it in the member initialization list:

struct SomeClass
{
    std::string bin[100];
    SomeClass() : bin() // You can initialize arrays with () in the mem-init list
    {
    }
};

*Technically, the default constructor will be called even if you don't add bin() to the mem-init list, but it is good practice (as built-in types will be left uninitialized).

In C++, members of a class are initialized through initialization lists. For example, in the following examples, there is a difference in the code:

// First example
struct MyClass
{
    int i;
    SomeClass() : i(1)
    {
    }
};

// Second example
struct MyClass
{
    int i;
    SomeClass()
    {
        i = 1;
    }
};

In the first example, i is initialized to 1 in the initialization list. In the second example, 1 is assigned to i. You could think of the above code as doing the following:

int i = 1; // First example

int i; // Second example
i = 1; 

For a built-in type like int there isn't much difference between the two, however the difference above can be very important in some situations, such as when the member is declared const:

struct SomeClass
{
    const int i;
    SomeClass()
    {
       i = 1; // Error!!
    }
};

In the above example, the const member must be initialized in the initialization list.

Sign up to request clarification or add additional context in comments.

10 Comments

What would I put in my class constructor? Since i declared string bin[100];as my member function would I just leave the initialization blank in the class constructor?
I hope bin is a member variable and not a function. Initialize it in the member initialization list of the ctor, for example MyClass::MyClass() : bin() {//body of ctor}
oops, I meant to say member variable. And I'm not really understanding what you're saying about initializing it in the list of the ctor. How would I do that? I edited my first post to include how my constructor looks like.
@user2419691: It seems you are not familiar with initialization lists. They are the C++ way of initializing members of a class. See this tutorial for some more information. In your code example, it would look like this: RandomString() : bin() { }.
@AvraamMavridis: someclass(std::string v[100]) is the same as someclass(std::string* v). You cannot use the pointer to initialize the member array. The best you can do is a for loop or some other construct like std::copy to copy each element into the array.
|
0

In your way, you just initialized the first element of the array, the default ctor initialize to null string. You want to initialize the 100 elements, just do the same thing for each of them.

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.