1
class A{
   private :  string a[3];
   public :   A();
              void ShowA();
}

A::A(){ string a[3] = {"aa","bb","cc"}  }
void A::ShowA(){
   for(int x=0;x<=2;x++){
       cout<< a[x];
   }
}
int main(){
    A a;
    a.ShowA();
    return 0;
}

In this code, I think the output is aabbcc but there is nothing. Just blank is existed. Could you tell me why it happens and how to fix it. Cheers guys.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
SoftWoo_is
  • 13
  • 2
  • 2
    `string a[3]` in your constructor creates a new local variable (completely unrelated to member variable with the same name), which is immidiately discarded. See [Initializing a member array in constructor initializer](https://stackoverflow.com/questions/4057948/initializing-a-member-array-in-constructor-initializer) or [C++ Initializing Non-Static Member Array](https://stackoverflow.com/questions/5643923/c-initializing-non-static-member-array) for a more modern approach. – Yksisarvinen May 21 '20 at 19:12
  • You're shadowing your own definition of `a` and assigning to a local variable that's later discarded. – tadman May 21 '20 at 19:12
  • 1
    you have a member called `a` and in the constructor you have an `a` that is local to the constructor. Read about scope (and `std::array` and initialization of members) – 463035818_is_not_an_ai May 21 '20 at 19:12
  • Consider using `std::vector` here. – tadman May 21 '20 at 19:12

2 Answers2

2

As the comments tell you, you are creating a local variable a inside your constructor, instead of setting the value of the attribute a. You can set the value of a in member initializer list.

The code becomes

#include <iostream>
#include <string>

using namespace std;

class A {
private:
    string a[3];

public:
    A();
    void ShowA();
};

A::A() : a{"aa"s, "bb"s, "cc"s} {}

void A::ShowA() {
    for(int x = 0; x <= 2; x++) {
        cout << a[x] << std::endl;
    }
}
int main() {
    A a;
    a.ShowA();
    return 0;
}

Note: The 's' after the "aa", "bb" and "cc" strings is a string literal. It is not really necessary in this case, since the compiler know you are creating an array of std::string objects.

darcamo
  • 3,294
  • 1
  • 16
  • 27
  • 1
    can ask `A::A() : a{"aa"s, "bb"s, "cc"s} {}` what is s mean?? – SoftWoo_is May 21 '20 at 19:30
  • You can always ask. That is the member initializer list. It is a way to initialize member variables. The syntax is ":" followed by variable name and its value between parenthesis or curly brackets. If you have multiple member variables you add a comma and then the next variable and so on. Notice that it is not in the constructor body (this has advantages). See the link for "member initializer list" in the answer. – darcamo May 21 '20 at 19:38
  • oh I missed last comments. I'll try to understand and think myself. Thanks a lot , Cheers! – SoftWoo_is May 21 '20 at 19:45
0

You are re-defining the string "a" in the constructor, you should simply assign it in the constructor instead of using the "string" type again. If you give the type again it creates a local variable available in the scope of the constructor only.