Solidity "Function needs to specify overridden contract" question
Asked Answered
G

2

6

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.

Grimonia answered 3/4, 2022 at 23:16 Comment(0)
K
3

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.

Kyrakyriako answered 4/4, 2022 at 7:16 Comment(0)
D
1

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.

Deltoid answered 4/4, 2022 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.