> For the complete documentation index, see [llms.txt](https://docs.applayer.com/testnet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.applayer.com/testnet/evm-contracts/calling-evm-contracts-from-cpp.md).

# Calling EVM contracts from C++

To invoke EVM contract functions from C++, we leverage a templated approach that mimics the contract's functions in a C++ class. This method provides a type-safe way to interact with contracts written in Solidity or other EVM-compatible languages.

First, we define a proxy C++ class that represents the EVM contract. This class will include stubs of the contract's functions, which do not contain actual logic but serve to match the contract's interface in the blockchain:

```cpp
class SolMyContract {
  public:
    uint256_t myFunction(const uint256_t& arg1, const uint256_t& arg2) const {};
    static void registerContract() {
      ContractReflectionInterface::registerContractMethods<SolMyContract>(
        std::vector<std::string>{},  // List of dependencies or related artifacts if any
        std::make_tuple("myFunction", &SolMyContract::myFunction, FunctionTypes::View, std::vector<std::string>{"arg1", "arg2"})
      );
    }
};
```

Then, we ensure that the proxy class is registered within the blockchain before any calls are made. Typically, this registration is done once, often in the constructor of the calling C++ contract, to set up the reflection system used for method invocation:

```cpp
uint256_t AnotherContract::callMyFunction(const Address& targetAddr, const uint256_t& arg1, const uint256_t& arg2) const {
  SolMyContract::registerContract();  // Ensure the EVM contract's methods are registered (Can be done only a single time in the constructor)
  return this->callContractViewFunction<SolMyContract>(this, targetAddr, &SolMyContract::myFunction, arg1, arg2);
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.applayer.com/testnet/evm-contracts/calling-evm-contracts-from-cpp.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
