1. 可行方法:
#include <iostream>
#include <string>
using namespace std;
//父类
class A {
public:
string s;
};
//子类
class B :public A {
B(string str){
A::s = str; //ok
this->s = str;//ok
}
};
int main() {
return 0;
}
2. 错误方法:
#include <iostream>
#include <string>
using namespace std;
//父类
class A {
public:
string s;
};
//子类
class B :public A {
B(string str) : s(str) {}//注意,此处,s(str)报错!!!
};
int main() {
return 0;
}