Home > Software engineering >  Solidity "Function needs to specify overridden contract" question
Solidity "Function needs to specify overridden contract" question

Time:04-04

I'm a newbie in Solidity, and I have a question about multiple inheritances.

So if I have some contracts like:

contract A {

    function foo() public virtual {
        console.log("A");
    }
}

contract B is A {
    function foo() public virtual override {
        console.log("B");
    }
}

contract C is A, B {
    function foo() public override(A, B) {
        super.foo();
    }
}

The foo function of contract C must be override(A, B) insdead override(B)

or it'd throw an error like Function needs to specify overridden contract "A".

So here's the question, The function must specify the full inheritance parents.

Why can't it know the information by contract C is A, B,

I mean, what's the point? The overrider(A,B) part is unnecessary.

Or there are some tricks I don't know?

Please give me an answer, so curious and can't find some useful information by docs.

CodePudding user response:

overriding implies that you're reimplementing a method you inherited!. So when you call this function inside child class, this reimplemented version of function will be called.

If you were just calling that function without reimplementing inside child class, you could call it because of this code:

   contract B is A {}

If you call a method that is not defined in B, compiler will check contract A and it finds it will call it.

CodePudding user response:

The official documentation of solidity says:

For multiple inheritance, the most derived base contracts that define the same function must be specified explicitly after the override keyword. In other words, you have to specify all base contracts that define the same function and have not yet been overridden by another base contract (on some path through the inheritance graph). Additionally, if a contract inherits the same function from multiple (unrelated) bases, it has to explicitly override it

So in your example you must use explicit override keyword.

  • Related