Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道构造函数的用途,并且有点知道数据隐藏的含义....发现两者之间绝对没有联系 (我是个笨蛋,sedd)....请帮忙?
我会争辩说,ctors 可以用作 RAII 的一种形式。本质上,我们可以通过构造函数初始化一个内部(私有)变量,并且现在可以使该变量在类之外无法访问。
class Foo { public: Foo(int i) : m_i(i) {} //< only place we init variable private: int m_i; //< we cannot access this var };