How does Attribute Inheritance in C++ Work?
问题 Suppose you wanted to reproduce the following Python snippet: class Base: name = "base" def announce(self): print(self.name) class Derived(Base): name = "derived" Base().announce() Derived().announce() ... which would output: "base" "derived" Initially, you may be inclined to write something like the following: #include <iostream> #include <string> struct Base { std::string name = "base"; void announce() { std::cout << name << std::endl; } }; struct Derived : public Base { std::string name =