c++ - Virtual non-method members -


Is this possible? This way it generates an error.

  class A {public: virtual std :: string key; }; Class B: Public A {Public: std :: string key; }; Int main (A); A.key = "Foo"; Return 1; }   

No, because it really does not make sense to remember that sub- In the class there are all members of its original class; Therefore, b is still a 's std :: string key . Also, since b is of std :: string key is the same type, it is very similar to a s - So, what was the issue of overriding it?

Also, note that during the construction, when we will not be asked the virtual methods of the creator of A , it means that If we arrive at the construction of A during the key , then we get the key of A - but then when B < / Code> is created, that key is shaded, its data is completely inaccessible.

He said, if you really want to do something like that, you will need to use a virtual accelerator function:

  class A {private: std: : String m_key; Public: Virtual std :: string and key () {return m_key; } Virtual const std :: string and key () const {returns m_key; }}; Class B: Public A {Private: std :: string m_key; Public: Virtual std :: string and key () {return m_key; } Virtual const std :: string and key () const {returns m_key; }}; Int main () {B b; B.K. () = "Foo"; Return 0; }  

Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -