简体   繁体   中英

C++ class inheritance

Im having a little problem with classes.

In code:

class A
{
public:
     int num;
     sayNum() { cout<<num;}
};

class B : public A
{
public:
     sayNum() { cout<<"my num is: "<<num;}
};

/*somewhere else...*/
A foo[10];

B something;
something.num=10;

foo[0]=something;

foo[0].sayNum();
/*...*/

When I call foo[0].sayNum(); it prints "10", and I would like it to print "my num is: 10". I can't change the type of the array. Note that it is just an example code, if I paste mine here it would be hard to understand :D)

This line:

foo[0]=something;

copies something into the array. Because the type of something (ie class B) is not the same as the type of foo[0] (ie class A), there is a conversion. In this case, something will be sliced , and only the A part will survive.

The net result is that foo[0] still contains an object with type class A , and thus the "my num is" will never be output.

Changing sayNum() to be virtual, which is good advice to solve other problems your code will show, won't help in this case.

How to solve it, depends on what you want to achieve. Possible solutions are:

  • Change the array to contain pointers : A* foo[10]; Note: this means you'll have to use 'new' and 'delete'.
  • Use a std::vector instead of an array: std::vector<A*> foo; Additional benefit: a vector can grow and shrink on demand.
  • Use smart pointers : std::vector<std::shared_ptr<A> > foo; Additional benefit: no worries about delete anymore.

But without knowing what you want to achieve, it is impossible to suggest the correct approach.

1. You should avoid slicing
You should use A* instead of A . This could help you understand why: What is object slicing?

2. You should declare sayNum() function of a class A as virtual
Definition of class A should look like this:

class A
{
public:
    int num;
    virtual void sayNum() { cout << num; }
};

Then your code as you say somewhere else could look like this:

A* foo[10];

B something;
something.num=10;

foo[0] = &something;

foo[0]->sayNum();

Declare the function sayNum to be virtual in the definition of A:

virtual void sayNum() { cout<<num;}

Edit: thanks to Sjoerd 's comment - this will still not help you. If you want to make use of the polymorphism you need to create an array of pointers to class A rather then an array of class A.

No, by assigning something to foo[0] you convert it to an A.

This is similar to assigning a float to an int - you lose all the decimals.

  1. Your missing the return types for your functions in each of your class. Say you want a return type to be void :

    void sayNum() { cout<<num;}

  2. When accessing the array elements, use the subscripts on array object :

    For example:

    foo[0]=something;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM