简体   繁体   中英

In C++ how to inherit a parent class' non-member functions, which is simply defined in the file?

I have codes like this:

class A{  // declaration is simplified
 virtual void FNC1();
};
bool compare(S s1,S s2){
    return  s1<s2; 
}
void A::FNC1(){
  iterator it;
  sort(it.begin(),it.end(),compare);
}

class B : public A{
 virtual void FNC1();
};
void B:FNC1(){
  iterator it;
  // do something different

  sort(it.begin(),it.end(),compare);
}

So I used class B to inherit class A and overrode the function FNC1(), but the problem is, as in the std::sort() function, the 3rd variable should really be a function, and such function is always directly declared. I really want to know what to do to avoid the copy and paste and make B directly inherit this function. I have tried to put the compare() function as a member function of A, and it won't compile: sort(it.begin(), it.end(), this->compare);

I tried to include the compare function into a separate header file, and it says I cannot declare it. How could I correctly make B inherits this function? Because, in practical, I have 3 classes all need to reuse A's codes and the compare function is really a complicated one.

Your problem is that function compare is defined in the header, meaning that you have its body there in addition to its signature. If you include the header in two places, the compiler will complain about multiple definitions. You should only have the declaration in the header, and the definition in a .cpp file.

This should go into A's header, let's call it ah

bool compare(S s1,S s2);

This should go into a.cpp

bool compare(S s1,S s2){
    return  s1<s2; 
}

By the way, just to clear up the terminology, you cannot inherit a non-member function. You can use any non-member function anywhere, as long as you include its declaration and link against its object file.

You could make the compare function a static member function of the base class, rather than making it free-standing:

class A{  // declaration is simplified
    virtual void FNC1();
public:
    static bool compare(const A& s1, const A& s2) {
        return ...; // The logic behind your compare function goes here
    }
};

You can use the function like this:

sort(it.begin(), it.end(), A::compare);

You are on the right track. You can simply reuse the compare function, you don't need to modify it or try to "inherit" it or any such thing.

The following should compile and run without error.

#include <algorithm>
#include <vector>

struct S { int i; };

class A{  // declaration is simplified
public:
 virtual void FNC1();
};
bool compare(const S& s1,const S& s2){
    return  s1.i < s2.i;
}

void A::FNC1(){
  std::vector<S> v;
  std::sort(v.begin(),v.end(),compare);
}

class B : public A{
public:
 virtual void FNC1();
};
void B::FNC1(){
  std::vector<S> v;
  // do something different

  std::sort(v.begin(),v.end(),compare);
}

int main () { A a; B b; a.FNC1(); b.FNC1(); }

The reason it won't compile if you make compare a member of A is probably that you are not making it public or protected. By default, members of a class are private, and a private member cannot be seen by a derived class.

You need:

class A{  // declaration is simplified
{
    virtual void FNC1();

    protected:
        bool compare( S s1, S s2 ){...}
};

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