4

I'm trying to learn Solidity and I came across the following 2 contracts:

https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_pong.sol https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_ping.sol

The contract pong holds some value, and the contract ping is supposed to be able to access this value via the getPongvalTransactional function. I'm confused with how this function works (disclaimer: I have no background in object oriented programming).

If I understand correctly, in the ping contract, all contracts of type PongvalRetriever contain a function called getPongvalTransactional which returns -1. The pong contract address is declared to be a contract of type PongvalRetriever.

However, the pong contract also contains a function called getPongvalTransactional which returns the actual value we want. In following line of the ping contract

function getPongvalRemote() 
{
    pongval = pvr.getPongvalTransactional();
}

why does pongval equal the actual pong value instead of -1?

1
  • pongval on the left of= is the variable that will receive the value on the right of =. On the right we have pvr, which is another instance which offers certain methods. One such is getPongvalTransactional(), which retrieves pvr's pong value, which in turn can have any value. You need OOP understanding to study Solidity. Sep 4, 2016 at 9:47

1 Answer 1

2

This is a technique called method overriding. Essentially, every subclass (in this case derived contract) of PongvalRetriever will contain by default a function getPongvalTransactional, which returns -1.

However, if a subclass, in this case the Pong contract, defines a method with the same name, that method "overrides" the superclass's method, so the method defined in Pong is what is actually executed.

Then, in the Ping contract, getPongvalRemote sets pongval to the return value of getPongvalTransactional, which should not be -1. The -1 is used to indicate an error, i.e. the function is for some reason not defined, so that 0 can be used as a legitimate value.

Not that pongval = pvr.getPongvalTransactional() is not making a statement about the equality of the two expressions, but is setting the value of pongval to the value of the right side of the expression.

2
  • I'm too new to upvote, but thank you! I have one other question: why do we need to go through the hoops of creating getPongvalTransactional() in the first place? The whole point is to access the pongval in pvr, so why can't we just do pongval=pvr.pongval? (and add whatever analogous error checking to PongvalRetriever)
    – ethhte
    Sep 4, 2016 at 14:32
  • You should be able to upvote now. If you have another question, please ask it in a separate thread. :-)
    – q9f
    Sep 5, 2016 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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