This is an old revision of the document!
An Accessor Method is an Operation defined by Object-Oriented Programming (OOP) providing access to Field Data. There are generally two Accessor Methods defined: get and set. In some Object-Oriented (OO) paradigms, the Accessor methods are further defined as:
Some benefits of using a Mutator Methods include:
In C++, the accessor methods are usually implemented as getter operations and the mutator functions are usually implemented as setter operation. The use of the prefix get and set at the beginning of a function is merely a programming convention and there is nothing within the C++ language to enforce the convention.
Examokle of using Getters and Setters
#include <iostream>
using namespace std;
class Account {
// Private attributes
private:
int balance;
// Public Accessor and Mutator operations
public:
// setBalance sets the balance in the account
void setBalance
( int _newBalance )
{
balance = _newBalance;
} // End setBalance
// getBalance returns the current balance in the account
int getBalance() {
return balance;
} // End getBalance
}; // End Account class
// main used as a unit tester of the Account class
int main()
{ Account account;
account.setBalance ( 50000 );
cout << myObj.getBalance();
return 0;
} // End Main tester
The following is an Ethereum Solidity example of a smart_contracts that uses Accessor and Mutator methods (i.e., Getters and Setters). Note: In this examole, there are no method (ie, function ) attributes which designate them as Getters or Setters other than the name convention. However, it is possible to use the View or Pure function attribution to help the compiler enforce the roles of each function type.
pragma solidity ^0.4.0;
// A simple smart contract
contract MessageContract
{
string message = "Hello World";
function getMessage() public constant
returns(string)
{ return message;
} // End getMessage
function setMessage(string newMessage) public
{ message = newMessage;
} // End setMessage
} // End MessageContract
Ethereum's Solidity can attribute Methods as View.
selfdestruct via callsGetter Method (i.e., Accessor Methods) are by default view methods.
Ethereum's Solidity can attribute Methods as Pure.
block, tx, msg (msg.sig and msg.data can be read).revert() and require() functions to revert potential state changes if an error occurs.