what's the difference between '_self' and '_code' in smart contract?
Hi, all. In the official example, I found these codes. ` [[eosio::action]] void erase(name user) { require_auth(user);
address_index addresses(_self, _code.value);
auto iterator = addresses.find(user.value);
eosio_assert(iterator != addresses.end(), "Record does not exist");
addresses.erase(iterator);
} `
What is the difference between '_self' and '_code'? In contract.hpp: _self: receiver - The name of this contract _code - The code name of the action this contract is processing.
/**
24 * Construct a new contract given the contract name
25 *
26 * @brief Construct a new contract object.
27 * @param receiver - The name of this contract
28 * @param code - The code name of the action this contract is processing.
29 * @param ds - The datastream used
30 */
31 contract( name receiver, name code, datastream<const char*> ds ):_self(receiver),_code(code),_ds(ds) {}
And i did some testing,bug i found _self and _code always the contract name...
/**
* The name of this contract
*
* @brief The name of this contract.
*/
name _self;
/**
* The code name of the action this contract is processing.
*
* @brief The code name of the action this contract is processing.
*/
name _code;
So normally in the main action self is equal to code. However, in the notifications and inline actions, code refers to the main action.
Please ask https://eosio.stackexchange.com/ if you have further questions.