0

Say you have a class Ball and in the Ball.h you have

class Ball {
public:
    int foo();
};

with its Ball.cc file having the function

int Ball::foo(){
    return 5;
}

and then you have a class BigBall with

#include <Ball.h>

class BigBall : public Ball {
public:

};

and then in your main you make a BigBall

auto bigRed = make_shared<BigBall>():

how would you go about using foo(); on the bigRed object?

somthing like

Ball::bigRed->foo();

but I get told that ball cant be resolved....

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
sam
  • 69
  • 6
  • You are using `auto`, `make_shared` but you have no idea what inheritance is? I would advice you to start with a beginner book to learn the basics. – Klaus Dec 17 '16 at 17:40

3 Answers3

1

Almost. You need to do the resolution on the function identifier, not the object:

bigRed->Ball::foo();

But unless BigBall overloads and hides the original Ball::foo, you don't really need to resolve anything:

bigRed->foo();
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
1

With std::shared_ptr you can use the -> operator like this:

auto foo = bigRed->foo();

Enjoy!

Stefano Azzalini
  • 1,027
  • 12
  • 21
1

All you need is this:

BigBall ball;
ball.foo();

That creates an object named ball of type BigBall and calls foo(). You don't have to do anything special to call a function that's defined in a base class. Just call it.

That auto and make_shared stuff is just a distraction for now.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165