C++开发EOS基础指南

栏目: C++ · 发布时间: 7年前

内容简介:为何选择C++?整个EOS区块链基础设施是用C++编写的。C++是一种低级语言,它使程序员可以很好地控制工作方式和管理资源。结果是,在诸如游戏,计算机图形之类的性能关键应用程序或大多数嵌入式系统等资源较少的硬件上,使用这个非常强大且高性能的语言。然而,将如此多的控制权转移给开发人员也使其成为最难学的语言之一。我们需要学习C++,因为你的EOS智能合约,你的去中心化应用程序的一部分,在区块链上,也必须用C++编写。然后将C++代码编译为WebAssembly。理论上,其他有“更容易”的语言可以编译为WebA

为何选择C++?整个EOS区块链基础设施是用C++编写的。C++是一种低级语言,它使 程序员 可以很好地控制工作方式和管理资源。结果是,在诸如游戏,计算机图形之类的性能关键应用程序或大多数嵌入式系统等资源较少的硬件上,使用这个非常强大且高性能的语言。然而,将如此多的控制权转移给开发人员也使其成为最难学的语言之一。

我们需要学习C++,因为你的EOS智能合约,你的去中心化应用程序的一部分,在区块链上,也必须用C++编写。然后将C++代码编译为WebAssembly。理论上,其他有“更容易”的语言可以编译为WebAssembly(最值得注意的是RUST,Python,Solidity),C++是Block One唯一官方支持的语言。

虽然这些其他语言可能看起来更简单,但它们的性能可能会影响你可以构建的应用程序的规模。我们希望C++将成为开发高性能和安全智能合约的最佳语言,并计划在可预见的未来使用C++。这里是 EOS开发人员门户网站

是的,C++是比较难的,当你的编程经验主要是通过像JavaScript这样的高级解释语言时,一开始可能看起来令人生畏——但这里有个好消息:编写智能合约实际上并不需要C ++的大部分功能。这些教程的目的是教您C++基础知识和智能合约编程实际需要的高级C++功能。

让我们花一点时间来承认和听到像JavaScript这样的高级语言没有的一些有用的现代C++特性。最为显着地:

typedef

如果你还不了解这些,请不要担心,我们将从基础开始。

基础

我假设你已经熟悉至少一种编程语言,如JavaScript或Python。然后理解定义变量, for 循环,如果 if 或C++中的调用函数等基础知识也不会让你感到意外。我们来看看语法:

// @url: https://repl.it/@MrToph/CPPBasics-1
// In c++ libraries are imported through the #include macro
// iostream comes with functions handling input and output to the console
#include <iostream>
// includes rand function
#include <cstdlib>
// includes time function
#include <ctime>

// this is how to define functions: <return type> <name>(<arguments>)
int compute(int x)
{
    // unsigned means no negative values which increases the range of numbers the variable can hold
    const unsigned int FIVE = 5;
    // FIVE = 3; // would throw an error as FIVE is declared constant
    return x * x + FIVE;
}

// the return type for no return value is called 'void'
void playGuessingGame()
{
    // initialize random number seed with time in seconds since the epoch
    srand(time(0));
    // get a random integer between 0 and 9 by doing modulus 10
    int random = std::rand() % 10;
    // need to initialize guess, uninitialized variables are indeterminate
    // meaning it could have any value, even the same as our random one!
    int guess = -1;
    std::cout << "Guess my number between 0 and 9";
    while (guess != random)
    {
        std::cin >> guess;
        if (guess > random)
            std::cout << "Lower";
        else if (guess < random)
            std::cout << "Higher";
        else
            std::cout << "Correct";
    }
}

// the entry point of your program is a function called main which returns an integer
int main()
{
    std::cout << "Hello! Type in a number\n";

    int number;
    std::cin >> number;

    int computed = compute(number);
    std::cout << "I computed x^2+5 as " << computed << "!\n";

    playGuessingGame();

    return 0;
}

整数 short,int,long,long long 有许多基本类型(每个都有一个 unsigned 替代,仅表示非负整数)。它们的区别在于字节数,保持整数范围。这里提到的这些类型没有指定的大小,它们的大小与实现有关。可能是如果你在一台机器上编译你的程序, int 有16位 (sizeof(int) == 2) ,而在另一台机器上编译时它将有32位。这些类型给你的唯一保证是最小字节数。例如, int 必须至少有16位, long 至少为32位。

使用数字时,了解各个类型的确切范围很有帮助,特别是在安全敏感的应用程序中,例如区块链开发,其中 over-/underflows 是关键。为了解决这个问题,C99添加了新类型,你可以在其中明确要求特定大小的整数,例如 int16_tint32_tunsigned uint64_t 变量。在编写智能合约时,我们将仅使用这些显式固定大小类型。

请注意,浮点数不存在类似的固定大小类型,因为位数不能很好地告诉你它的精度和范围。在这些情况下,你需要使用 float,double,long double (前者通常是32位和64位IEEE-754浮点类型)。

字符串

除了数字类型和布尔 bool 类型之外, string 是最常用的数据类型之一。它们通过 <string> 包含在 std 命名空间中。(命名空间是变量的区域范围,是解决大型项目中名称冲突的一种方法。)

// @url: https://repl.it/@MrToph/CPPBasics-Strings
#include <iostream>
// need to import <string> for strings
#include <string>

int main()
{
  // strings are part of the std namespace
  std::string text = "Hello";
  // + is used for concatenation
  text += " World";
  std::cout << text << "\n";
  // length and size are synonyms
  std::cout << text.length() << " " << text.size() << "\n";

  text = text.substr(0, 5);
  for (int i = 0; i < text.length(); i++)
  {
    std::cout << i << ": " << text[i] << "\n";
  }

  // different way to loop over characters
  for (char c : text)
  {
    std::cout << c << "\n";
  }
  return 0;
}

数组/向量

C++区分静态和动态数组。静态数组是具有固定大小的数组,在编译时是已知的。如果你的数组需要能够增长或仅在运行时知道大小,则需要使用向量 vectors

// @url: https://repl.it/@MrToph/CPPBasics-Vectors
#include <iostream>
#include <vector>

int main()
{
    // arrays are defined with [] after the variable name
    // and can be immediately initialized providing values in { ... }
    int arr[] = {1, 2, 3};
    // can specify size in brackets
    // initializes elements in the list, rest to 0
    int brr[3] = {1, 3};
    for (int x : brr)
    {
        // outputs 1, 3, 0
        std::cout << x << "\n";
    }

    std::vector<int> numbers;
    for (int i = 0; i < 3; i++)
    {
        // add a number to the back
        numbers.emplace_back(i);
    }
    // size and accessing vectors is the same as with arrays
    std::cout << "numbers: " << numbers[0] << numbers.size() << "\n";

    // this inserts a number at the second (index 1) place
    numbers.emplace(numbers.begin() + 1, 9);
    // numbers.begin returns an iterator. More on these later
    for (std::vector<int>::iterator it = numbers.begin(); it != numbers.end(); it++)
    {
        // outputs 1, 3, 0
        std::cout << *it << "\n";
    }
}

今天就到这,在下一个教程中,我们将讨论将参数传递给函数的不同类型。

======================================================================

分享一个交互式的在线编程实战, EOS智能合约与DApp开发入门

EOS教程

本课程帮助你快速入门EOS区块链去中心化应用的开发,内容涵盖EOS工具链、账户与钱包、发行代币、智能合约开发与部署、使用代码与智能合约交互等核心知识点,最后综合运用各知识点完成一个便签DApp的开发。

  • java比特币开发教程,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在 Java 代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Java工程师不可多得的比特币开发学习课程。
  • java以太坊开发教程,主要是针对java和android程序员进行区块链以太坊开发的web3j详解。
  • php比特币开发教程,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在 Php 代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Php工程师不可多得的比特币开发学习课程。
  • php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和交易等内容。
  • 以太坊入门教程,主要介绍智能合约与dapp应用开发,适合入门。
  • 以太坊开发进阶教程,主要是介绍使用node.js、 mongodb 、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • python以太坊,主要是针对 python 工程师使用web3.py进行区块链以太坊开发的详解。
  • C#以太坊,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和交易等。

汇智网原创翻译,转载请标明出处。这里是 原文


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Types and Programming Languages

Types and Programming Languages

Benjamin C. Pierce / The MIT Press / 2002-2-1 / USD 95.00

A type system is a syntactic method for automatically checking the absence of certain erroneous behaviors by classifying program phrases according to the kinds of values they compute. The study of typ......一起来看看 《Types and Programming Languages》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具