0

Question in one sentence: Solidity does not support the variable overriding?

Consider a following code:

contract B { 
  function set() {} 
  function f() { 
    set(); 
  } 
}

contract A is B { 
   function set() { }
}

Supposing the contract A is deployed, when we call f(), the function set() inside A (not B) will be called, because the most derived function set() is defined in A (i.e., the function set() is overridden).

My confusing part is the following. Let me show another example:

contract B{
    uint public n;

    function test2() public { n = n + 1; }
}

contract A is B{
    uint public n;
}

Based on the previous example, I guessed that if the deployed contract is A and we call the function test2(), the variable n inside A would be increased, because the variable is overridden.

However, when I tried the above example in Remix, I found that my guess was wrong. It seems that Solidity supports the function overriding, but not the variable overriding.

2

Solidity does not support variables override, nor operators override, in the sense you are asking, but function override only.

In particular variables can be eventually "shadowed" (i.e. both the two variables with the same name continue to exist, but one of them is no longer reachable by the code), but never overriden (i.e. the compiler do not unify two variables with the same name in a unique memory location).

Functions override is an obvious result of Solidity mechanisms, given the ABI mechanism of function call, variables override shall require an additional layer to the syntactic analysis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.