class - C++ Override base member value -


i have following code:

class { public:     int foo = 0; };  class b: public { public:     int foo = 1; };  int main() {     *a = new b();      std::cout << a->foo;     std::getchar();      return 0; } 

output: 0

why b not override member foo ?

and how desired output of 1 without casting derived class

b::foo separate member a::foo. why initializing b::foo not update a::foo. have more instead:

class { public:     int foo;     a(int value = 0) : foo(value) {}  };  class b : public { public:     b() : a(1) {} };  int main() {     *a = new b();     std::cout << a->foo;     delete a;     std::getchar();     return 0; } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -