Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"
}


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:

____________________________________________-
Posted
Updated 28-Jun-18 2:43am

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,
C++
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"
}

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,
C++
string _whose_move = "white";
string _whose_move("white");

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

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,
C++
// 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;
}

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 {

It starts to complain, and so on and so forth. So the thing is, you have to follow the C++ standards and the syntax, otherwise there are problems. Serious ones.

Final program for you to try out and test; C++ Shell[^]
 
Share this answer
 
Comments
BerthaDusStuf 28-Jun-18 5:29am    
Ok but what is the difference between passing "white" into the constructor and setting the string equal to white when it is declared?
Afzaal Ahmad Zeeshan 28-Jun-18 7:07am    
Please reread the answer, the difference is one is being done before the object is done initialized, and other one is just like a setter function.
BerthaDusStuf 28-Jun-18 9:54am    
yes I read your answer quite alot but im struggling to understand what you mean.
For example here:
"He was initializing the field at the time of object creation, rather after that."
my question is well wouldnt doing string whose_move = "white" also be initialising the field at the time of object creation?
Afzaal Ahmad Zeeshan 28-Jun-18 11:20am    
I understand, the concepts are a bit different in C++ and with C++ it is more complex than you can think, with Java or C# it is much simpler. But in C++, there is a concept of RAII (resource allocation is initialization). Which means, that when you are allocating a resource it is initialized, rather the opposite — to initialize later as you are referring to, with a string name = "something";

So in other terms,

// Notice we are not passing anything
ChessBoard::ChessBoard () : _whose_move()
{
}

is similar to what we have with

ChessBoard::ChessBoard ()
{
}
Compiler will implicitly have the string variables initialized with the default values, and passing white means that the default value for them will be a white.

Please see the following links for a bit more explanation on this topic,
https://www.quora.com/In-C++-why-are-class-members-initialized-with-their-default-constructor-before-the-constructor-body-executes-instead-of-the-compiler-detecting-which-members-werent-initialized-during-the-constructor-and-initializing-them-with-the-default-constructor-afterwards#
https://stackoverflow.com/questions/9903248/initializing-fields-in-constructor-initializer-list-vs-constructor-body
BerthaDusStuf 29-Jun-18 18:18pm    
ok thanks
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:
C++
ChessBoard::ChessBoard() : _whose_move()
{
}
Hint: all other members of the class too.

For set the string you need some code, which isnt optimal:
C++
ChessBoard::ChessBoard()
{
 _whose_move = "white";
}
You can proof this by stepping into the constructor code with the debugger, when a ChessBoard gets constructed. :-O
 
Share this answer
 
Comments
BerthaDusStuf 28-Jun-18 3:49am    
sorry i dont understand very well
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
C++
#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();
}
 
Share this answer
 
Comments
Nelek 28-Jun-18 9:11am    
Tag is C++11 ;)
BerthaDusStuf 29-Jun-18 18:20pm    
ok thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900