What actually happens with the style of the address space style for cast objects?

According to the book, the C ++ FAQ from Cline, the following code shows incorrect inheritance. note that I can add a banana, which is a kind of fruit in bagofApples that should only contain apples .. but due to inheritance relationships, banana is added to bagofApples.

but the question is what actually happens on this line:

 { return (Apple&) BagOfFruit::remove(); }//what does happen here?

What does c-style type listing do? note that the sizeof (banana) is 4004, and the apple is only 4. so that although the delete function of the banana object gets access to the apple object, because the address offset of the apple function will be comparable to the banana function, but when the banana is assigned to the apple object what happens to the ban [ar] 1000 data element? where does it lie in memory for apple & the link here is what happens to the address space of the cast banana object (the actual object)?

Apple& a2 = bagOfApple.remove();

The following is the full code.

#include "stdafx.h"


#include <iostream>
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 << "Apple\n"; }

class Banana : public Fruit 
{
    public:
    virtual void printClassName() const throw();
    protected:
        int ar[1000];
};

void Banana::printClassName() const throw()
{ cout << "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& f) throw(Full);
    Fruit& 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& f) throw(Full)
{
    if (size_ == maxSize_) throw Full();
    data_[size_++] = &f;
}

Fruit& BagOfFruit::remove() throw(Empty)
{
    if (size_ == 0) throw Empty();
    return *data_[--size_];
}


void insertFruitIntoBag(BagOfFruit& bag, Fruit& fruit)
{
bag.insert(fruit);
}

class BagOfApple : public BagOfFruit {
public:
BagOfApple() throw();
void insert(Apple& a) throw(Full);
Apple& remove() throw(Empty);
};

BagOfApple::BagOfApple() throw()
: BagOfFruit()
{ }

void BagOfApple::insert(Apple& a) throw(Full)
{ BagOfFruit::insert(a); }

Apple& BagOfApple::remove() throw(Empty)
{ return (Apple&) BagOfFruit::remove(); }//what does happen here?


int _tmain(int argc, _TCHAR* argv[])
{
    BagOfApple bagOfApple;
    Banana banana;
    insertFruitIntoBag(bagOfApple, banana);
    cout << "Removing an Apple from bagOfApple: ";
    Apple& a2 = bagOfApple.remove();
    a2.printClassName();
    return 0;
}
+3
source share
3 answers

Firstly, if this code comes from a book, you will get another book:

  • , BagOfApples BagOfFruit ( , , BagOfApples BagOfFruit, , )
  • :
    • ... , . insert() , , . .
    • , ( - , - ).
    • remove() , . Comp Sci pop() .

, :

{ return (Apple&) BagOfFruit::remove(); }//what does happen here? 

, remove() "" /, C- promises , - Apple. , , BagOfApples, , BagOfFruit, , - BagOfFruit - . Apple&, Apple, - Apple, undefined.

, , , , . , "const char *" , . , , a Banana: ar[0] , , ar[1] ( 32- int 64- ) const char*, . , . , , , undefined .

+3

" c- ?": Lying. , .

C , . Banana, , Fruit, a static_cast; , reinterpret_cast. , ( , Apple) undefined . , , , — , Banana. a Banana, ; , , . : - end Apple, , , , . Apple Banana , undefined , .

+4

c- ?

. , . , ( ). c-style cast ++. , , . . .

, apple,

.

where does it lie in memory for apple & the link here is what happens to the address space of the cast banana object (the actual object)?

Nothing. In this case, castings do not change anything, because it is a cast to a reference type, therefore it creates a link. They simply tell the compiler not to complain about types. If you need c-style in C ++, you are probably something wrong.

+1
source

All Articles