4

is there a possibility to create a base contract with methods that can be overridden in contracts that inherits from it?

Cheers

3
  • Have you tried anything so far that is not working or is this a general question?
    – TeeAttack42
    Jul 17 '18 at 9:43
  • I tried this situation and it gets crazy with namespaces, or it says that action is already defined etc... My question would be is this possible at all, are methods in base class supposed to be virtual and maybe if someone can create small example with one method in base class and how it is referenced from inherited? :)
    – Bida
    Jul 17 '18 at 11:33
  • I guess that this is not supported through the compiler to web assembly (wasm).
    – friedger
    Jul 17 '18 at 21:38
1

I did a fair bit of experimenting and I found a way where inhering within the contract class (sort of) works. Although it doesn't allow for the actions from one contract to be used in another, it does allow the internal functionality.

Below is an example of inheritance used within a smart contract:

inherited.hpp

#ifndef INHERITED
#define INHERITED

#include <eosio/eosio.hpp>
using namespace eosio;

class inherited
{
   public:
      ACTION hi2( name nm );
};
#endif

mycontract.hpp

#include <eosio/eosio.hpp>
#include <inherited.hpp>
using namespace eosio;

CONTRACT mycontract : public inherited, public contract {
   public:
     using contract::contract;
     ACTION hi( name nm );
};

mycontract.cpp

#include <mycontract.hpp>
#include <inherited.hpp>

ACTION inherited::hi2( name nm ) {
   /* fill in action body */
   print_f("Name : %\n",nm);
}

ACTION mycontract::hi(name nm){
  eosio::print("Test");
  hi2(nm);
}

The ABI generator does not recognise the action hi2, but you can use the function within your code without issue.

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.