c++ - Does derived class allocate memory for member variable? -
#include<cstdio> #include<iostream> using namespace std; class { public: int x; }; class b: public { }; int main() { b b; b.x=5; cout<<b.x<<endl; homecoming 0; }
i have above code.it's okay.but want know when inherit class b class fellow member variable x declared in class b or class b access fellow member variable x of class ? there 2 variables same name in 2 different classes or there 1 variable , objects of both classes have access ? if there 2 different variables same name in 2 different classes why, when object of derived class declared constructor of base of operations class called ?
when create object of derived class, base of operations class sub-object embedded in memory layout of derived class object. so, question, there's on variable part of derived object. since, taking non-static members here, each derived object gets base-class sub-object laid out in memory. when create base of operations class object, different piece of memory representing different object , has nil derived object created earlier.
hope clarifies doubt!
this great book understand c++ object model:
http://www.amazon.com/inside-object-model-stanley-lippman/dp/0201834545/ref=sr_1_1?ie=utf8&qid=1412535828&sr=8-1&keywords=inside+c%2b%2b+object+model
c++ class inheritance
No comments:
Post a Comment