1.下面代码有问题吗?
#include <iostream>
#include <string.h>
using namespace std;
class A
{
public:
A(char * ps)
:name(ps), len(strlen(name.c_str())) {}
void dis()
{
cout << len << endl;
}
private:
int len;
string name;
};
int main()
{
A a("china");
a.dis();
return 0;
}
问题在哪?
天蓬老师2017-04-17 14:42:53
Write the data member string name; in front of int len;. Because data members are initialized in the order of declaration. If the name has not been initialized, how can you use the len of the name.