关于类构造函数,我不明白这一点 [英] I dont understand this about classes constructors

查看:83
本文介绍了关于类构造函数,我不明白这一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在阅读的书跳入C ++中有一些我不理解的东西。以下是代码:



In the book I am reading "jumping into C++" there is some stuff I dont understand. Here is the code:

class ChessBoard
{
public:
ChessBoard ();
string getMove ();
ChessPiece getPiece (int x, int y);
void makeMove (int from_x, int from_y, int to_x, int to_y);
private:
PlayerColor _board[ 8 ][ 8 ];
string _whose_move;
};





这是整个类声明,但接下来是构造函数实现:





That was the whole of the class declaration but then here is the constructor implementation:

ChessBoard::ChessBoard ()
// the colon is followed by the list of variables, with the argument
// to the constructor
: _whose_move( "white" )
{
// at this point, _whose_move constructor has been called and it
// contains the value "white"
}





首先,这个人将白色放入其运动的构造函数中。这有什么意义,因为他的动作是一个字符串所以你可以这样做:



string which_move =white



其次,who_move是在函数括号之外声明的,为什么?



我尝试了什么:



____________________________________________-



Firstly, the guy puts "white" into the constructor of whose_move. What is the point of this as whose move is a string so you can just do:

string whose_move = "white"

Secondly, whose_move is declared outside of the brackets of the function, why?

What I have tried:

____________________________________________-

推荐答案

因为构造函数在C ++中不能以这种方式工作,所以一旦调用构造函数就会被调用对象已创建(或已创建)。这意味着,正文中的代码应在对象创建后执行,仅用于验证目的。



在C ++中,你可以用这种方式初始化对象,所以当他这样做时,

Because a constructor does not work that way in C++, constructors are called once the object is created (or has been created). That means, that the code in the body shall execute after object creation, merely for validation purposes or so.

In C++, you can initialize the object that way, so when he did,
ChessBoard::ChessBoard ()
// the colon is followed by the list of variables, with the argument
// to the constructor
: _whose_move( "white" )
{
// at this point, _whose_move constructor has been called and it
// contains the value "white"
}



他正在初始化对象时的字段创作,而不是之后。而且who_move基本上是一个字符串,再一次,在C ++中你可以轻松地创建变量,例如,


He was initializing the field at the time of object creation, rather after that. And whose_move is basically a string, and once again, in C++ you can easily create the variables like,

string _whose_move = "white";
string _whose_move("white");

// I know pointers involved here
string _whose_move = new string("white");



这完全取决于你想做什么,以及你想如何编写程序,试试我为你写的代码示例; C ++ Shell [ ^ ],它写出了我的名字,代码如下,


That all depends on what you want to do, and how you want to write the program, try this code sample I wrote for you; C++ Shell[^], it writes out my name, the code is as following,

// Example program
#include <iostream>
#include <string>

int main()
{
  std::string name("Afzaal Ahmad Zeeshan");
  std::string name2 = "Afzaal Ahmad Zeeshan";
  std::string* nameRef = new std::string("Afzaal Ahmad Zeeshan");
  std::cout << "Hello, " << name << " from the constructor-like!\n" << std::endl;
  std::cout << "Hello, " << name2 << " from the variable!\n" << std::endl;
  std::cout << "Hello, " << *nameRef << " from the pointer!\n" << std::endl;
}



在这里我们有三种方法来编写变量,你可以自己检查一下。现在将这个概念带回C ++构造函数,并添加C ++语法支持的混合,您可以轻松地看到该域中可以支持哪些。



现在看到这个,


In this we have three ways to write the variable, you can check for yourself. Now take this concept back to the C++ constructor, and add the blend of syntax support of C++, you can easily see which of these can be supported in that domain.

Now see this,

class person {
public:
    std::string name;  
    person(std::string);
};

person::person(std::string n) : name(n) {
    std::cout << "Person created with name " << n  << "." << std::endl;
}



尝试这样做,


Try doing this,

person::person(std::string n) : name = n {



它开始抱怨,依此类推。所以问题是,你必须遵循C ++标准和语法,否则就会出现问题。严肃的。



最后的程序供您试用和测试; C ++ Shell [ ^ ]


解决方案是正确的,因为他将字符串放入字符串的构造函数中。如果没有,那么会执行标准构造函数,它会在构建阶段注入编译器。



想象一下:

The solution is correct, because he was putting the string into the constructor of the string. If not there would executed the standard constructor, which injects the compiler in the build phase.

Imagine it as:
ChessBoard::ChessBoard() : _whose_move()
{
}

提示:该类的所有其他成员也是。



设置字符串需要一些代码,这不是最佳的:

Hint: all other members of the class too.

For set the string you need some code, which isnt optimal:

ChessBoard::ChessBoard()
{
 _whose_move = "white";
}

当构建ChessBoard时,您可以通过使用调试器进入构造函数代码来证明这一点。 :-O

You can proof this by stepping into the constructor code with the debugger, when a ChessBoard gets constructed. :-O


C ++ 17 之前,初始化列表是使用默认值初始化类成员的正确方法。使用 C ++ 17 ,您可以直接在类体中提供默认初始化。尝试使用现代编译器

Prior to C++17 the initializer list was the proper way to initialize class members with default values. With C++17 you can provide default initialization directly in the class body. Try, with a modern compiler
#include <iostream>
using namespace std;

class ChessBoard
{
public:
  ChessBoard (){}
  string getMove ();
  void makeMove (int from_x, int from_y, int to_x, int to_y);
  void showWhoseMove(){ cout << _whose_move << "\n"; }
private:
  string _whose_move = "white"; // allowed only since C++17
};

int main()
{
  ChessBoard cb{};
  cb.showWhoseMove();
}


这篇关于关于类构造函数,我不明白这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆