what actually happens during a c- style typecasting regarding address space of casted objects? <p>According to book, C++ FAQ by Cline,the following code depicts improper inheritance. notice that i can add a banana which is a kind-of fruit into a bagofApples which should actually contain only apples.. but due to the inheritance relationship banana is added to the bagofApples.</p> <p>but the question is what actually happens at this line:</p> <pre><code> { return (Apple&amp;) BagOfFruit::remove(); }//what does happen here? </code></pre> <p>what does the c-style type casting do here? notice that the sizeof(banana) is 4004,while that of an apple is only 4. so though the remove function of the banana object is accessed by the apple object because the apple's function's address offset would be matching with that of banana's,but when the banana is typecasted to an apple object what happens to the data member ar[1000] of the banana? where does it lie in memory for the apple&amp; reference here,what happens to the address space of the casted banana object(actual object)?</p> <pre><code>Apple&amp; a2 = bagOfApple.remove(); </code></pre> <p>The full code follows.</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; using namespace std; class Fruit { public: virtual void printClassName() const throw() = 0; virtual ~Fruit() throw(); }; Fruit::~Fruit() throw() { } class Apple : public Fruit { public: virtual void printClassName() const throw(); }; void Apple::printClassName() const throw() { cout &lt;&lt; "Apple\n"; } class Banana : public Fruit { public: virtual void printClassName() const throw(); protected: int ar[1000]; }; void Banana::printClassName() const throw() { cout &lt;&lt; "Banana\n"; } //The following BagOfFruit class allows insertion and removal of objects of any //kind-of Fruit. class Full { }; class Empty { }; class BagOfFruit { public: BagOfFruit() throw(); unsigned size() const throw(); void insert(Fruit&amp; f) throw(Full); Fruit&amp; remove() throw(Empty); protected: enum { maxSize_ = 20 }; unsigned size_; Fruit* data_[maxSize_]; }; BagOfFruit::BagOfFruit() throw() : size_(0) { } unsigned BagOfFruit::size() const throw() { return size_; } void BagOfFruit::insert(Fruit&amp; f) throw(Full) { if (size_ == maxSize_) throw Full(); data_[size_++] = &amp;f; } Fruit&amp; BagOfFruit::remove() throw(Empty) { if (size_ == 0) throw Empty(); return *data_[--size_]; } void insertFruitIntoBag(BagOfFruit&amp; bag, Fruit&amp; fruit) { bag.insert(fruit); } class BagOfApple : public BagOfFruit { public: BagOfApple() throw(); void insert(Apple&amp; a) throw(Full); Apple&amp; remove() throw(Empty); }; BagOfApple::BagOfApple() throw() : BagOfFruit() { } void BagOfApple::insert(Apple&amp; a) throw(Full) { BagOfFruit::insert(a); } Apple&amp; BagOfApple::remove() throw(Empty) { return (Apple&amp;) BagOfFruit::remove(); }//what does happen here? int _tmain(int argc, _TCHAR* argv[]) { BagOfApple bagOfApple; Banana banana; insertFruitIntoBag(bagOfApple, banana); cout &lt;&lt; "Removing an Apple from bagOfApple: "; Apple&amp; a2 = bagOfApple.remove(); a2.printClassName(); return 0; } </code></pre>