1

I have some code that looks like this:

// original function
size_t computeAverageRating(Movie m) { // take a movie object
    // compute average rating
}

// override original function
size_t computeAverageRating(size_t movieIndex) { // take the index of a movie
    getMovieObject(movieIndex);

    // call original function from here?
}

Is it possible to do what is being described in the comments without creating two separate methods with different names?

1
  • Yes. That's what overloaded functions are for. Commented May 1, 2014 at 16:20

4 Answers 4

4

Assuming getMovieObject() actually returns a Movie (although I'd change computeAverageRating() to take Movie &m to avoid copying the object):

size_t computeAverageRating(size_t movieIndex) { // take the index of a movie
  Movie m = getMovieObject(movieIndex);

  return computeAverageRating( m ) ;
}

Should do it.

Sign up to request clarification or add additional context in comments.

2 Comments

You surely mean const Movie &m?
How can i call the overloaded method without compiler error, if I can't change it to Movie& m
2

Should work, its a good method so why do you want to look for another method?

size_t computeAverageRating(size_t movieIndex) {
    return computeAverageRating(getMovieObject(movieIndex));
}

Comments

1

What you have there is perfectly fine. The compiler will choose the best function based on the parameters, so for example:

size_t z;
Movie x;
size_t a = computeAverageRating(x);
size_t b = computeAverageRating(z);

Will get the average rating from the first function for a, then the second function for b.

Comments

0

As per my knowledge, if the function is defined in the parent class, then you can't use the same function name with different parameter in child class without overriding the parent class function.

If you want to use then you have to either override the method defined in the parent class, or you need to put the declaration of new function definition in parent class.

ex:

class A
{
public:
    size_t computeAverageRating(Movie m) { // take a movie object
    // compute average rating
    }
};

class B:public A
{
    public:
    size_t computeAverageRating(Movie m)   //this is required
    {
        A::computeAverageRating(m);
    }
    size_t computeAverageRating(size_t movieIndex)
    {
           getMovieObject(movieIndex);
           //put your logic here
    }
};

or

class A
{
public:
    size_t computeAverageRating(Movie m) { // take a movie object
    // compute average rating
    }
    virtual size_t computeAverageRating(size_t movieIndex){};
};

class B:public A
{
    public:
    size_t computeAverageRating(size_t movieIndex)
    {
           getMovieObject(movieIndex);
           //put your logic here
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.