0

I am coding in C++11 and I have a large parent non-static function and I want to reuse it in my derived class.

The parent non-static function uses a static function defined in itself. I have overloaded the static function in my derived class and I want the parent non-static function to use the static function of the derived class. But it doesn't and uses its own version. A similar question was asked but this is different in the sense that it deals with static as well as non-static members of a class. For example, see the sample code below:

#include <iostream>
class Base
{
    public:
    static int value() { return 0; }
    int getValue() { return value(); }
};
class Derived:public Base
{
    public:
    static int value() { return 1; }
};
int main()
{
    Derived obj;
    std::cout << obj.getValue() << std::endl;
    return 0;
}

I want the code the print 1 but it prints 0

Is there any way I can do that? Thanks a lot for helping.

Update #1

For me, the getValue() function is quite big, which is why I asked the question in the first place. Overriding the getValue() function in the derived class, as suggested in the comments might be a feasible option but for me, it would be copying a large function multiple time, since I have multiple derived classes, which would make it cumbersome to make changes in the future. Briefly saying,

I want to define a non-static, public function once in a parent class which uses a static member of the class, and then reuse the non-static function in every child class in such a way that in case I overload the static function of the parent class in the child class, the non-static function of the parent class uses the overloaded function.

  • Why don't you override `getValue()` in `Derived` class? – Azeem Jan 15 '20 at 08:18
  • Extract the common functionality in `protected` part of `Base` class. Use it in both in `Base` and `Derived` classes to avoid repetition by defining a function in `Base` and override it in `Derived`. – Azeem Jan 15 '20 at 08:33
  • @Azeem, overriding `getValue()` in `Derived` class might be helpful. But this is just a sample code. In my case, `getValue()` is a very big function. I want a solution other than copy-pasting the same function in a different class. And also, I cannot declare the common functionality as `protected` as I need to use it as `public`. – NullPointer Jan 15 '20 at 09:10
  • If that is the case, you should at least post an example that completely describes the problem satisfying all your use-cases. That would be more helpful in getting better responses and clarifying that it's not an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and/or a broken design issue. – Azeem Jan 15 '20 at 09:16
  • Thanks @Azeem. Changes reflected. – NullPointer Jan 15 '20 at 16:44
  • Right. Please add the signature of that function also. Can you modify the parent class? It still sounds like that the answer is what I said in my [comment](https://stackoverflow.com/questions/59747202/c-call-derived-class-static-function-in-base-class-non-static-function?noredirect=1#comment105642105_59747202) above. – Azeem Jan 16 '20 at 04:10
  • The long function would remain there but its functionality would be refactored out to another function which you can later use in child classes. Its scope `public` or `protected` would depend on your use-case. – Azeem Jan 16 '20 at 04:37
  • Yes, I can modify the parent class. But I need to declare the common functionality as `public` since I need to use it without inheriting it. – NullPointer Jan 17 '20 at 05:06
  • Right. Yes, you can have a `public static` function with common functionality and use that directly without inheriting. – Azeem Jan 17 '20 at 08:15
  • Could you give a snippet? – NullPointer Jan 18 '20 at 06:52
  • You're already doing something similar. You just need to override `getValue()` in your `Derived` class. Snippet: https://godbolt.org/z/sNaQCx. You might need to extract some functionality out to another function in `Base` depending on your use-case (as you mentioned earlier that it's a very large function so some refactoring would be required to reuse it other places). – Azeem Jan 18 '20 at 09:46

0 Answers0