c++ - Member pointer to member object and declaration order -


#include <iostream>  class fooparent {     public:         fooparent(int* new_p_bar)         {             p_bar = new_p_bar;         }     public:         int* p_bar; };  class foochild : public fooparent {     public:         int bar;     public:         foochild(int new_x)         :fooparent(&bar)         ,bar(new_x) \\ point of concern             {}  };  int main() {      foochild foo(8);     std::cout << foo.bar << std::endl;  } 

the above example works want .i.e. link pointer p_bar bar. however, concern pointing member constructor not yet called.

is code valid, or standard have it. if not alternative.

note: in application bar object bar (not int), have implications?

look @ this:

class fooparent {     public:         fooparent(int* new_p_bar)         {             p_bar = new_p_bar;             *p_bar = 99; // has no sense         }         void set99() {             *p_bar = 99; // - has         }     public:         int* p_bar; };  class foochild : public fooparent {     public:         int bar;     public:         foochild(int new_x)         :fooparent(&bar)         ,bar(new_x) // point of concern             {}  };  int main() {      foochild foo( 42 );     std::cout << foo.bar << std::endl;     foo.set99();     std::cout << foo.bar << std::endl; } 

lws.

i mean if fooparent's constructor only stores pointer external int (or bar - doesn't matter) there no problem.

in other hand, if you'll give copy of bar fooparent - this

class fooparent {     public:         fooparent(bar new_p_bar)         {             p_bar = new_p_bar;         }         void set99() {             p_bar = 99; // - has         }     public:         bar p_bar; };  class foochild : public fooparent {     public:         bar bar;     public:         foochild(bar new_x)         :fooparent(bar)         ,bar(new_x) // point of concern             {}  };  int main() {      foochild foo( 42 );     std::cout << foo.bar << std::endl;     foo.set99();     std::cout << foo.bar << std::endl; } 

lws.

this not work. if bar have copy c-tor or/and assignment operator


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -