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.
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 or C++ Initializing Non-Static Member Array for a more modern approach.aand assigning to a local variable that's later discarded.aand in the constructor you have anathat is local to the constructor. Read about scope (andstd::arrayand initialization of members)std::vectorhere.