如何为类中的常量赋值
#include <string> #include <iostream> using namespace std; class A{ const string name; public: A(const string& id=""):name(id){} ~A(){} void print(){ cout << name<<endl; } }; class B{ string name; public: B(const string id){name=id;} ~B(){} void print(){ cout << name<<endl; } }; int main(){ A aa("aa"); aa.print(); B bb("bb"); bb.print(); }