0

I'm reading -

Thinking in C++, Volume 1, 2nd Edition by Bruce Eckel

Consider the class given in the book -

class Game {
    GameBoard gb; // Composition
public:

  ...
    class Other{
    public:
        Other() { Log(std::cout); }
        ~Other() { Log(std::cout); }
    }; // Nested class

    // Automatic type conversion:
    operator Other() const { Log(std::cout); return Other(); }

    ~Game() { Log(std::cout); }
};

Wondering how can I use operator Other() ?

Please help .

Onkar N Mahajan
  • 410
  • 3
  • 13
  • 1
    `Game::Other other = game;` – 康桓瑋 Feb 13 '22 at 08:42
  • 1
    Could you clarify what you want to do and what exactly doesn't work for you? – user17732522 Feb 13 '22 at 08:42
  • @user17732522 I want to know how can operator Other() could possibly be used ? – Onkar N Mahajan Feb 13 '22 at 08:49
  • 1
    @OnkarNMahajan Like any other conversion function. It is not clear to me what about it being a nested class changes anything about it. But the first comment gave an example using it. – user17732522 Feb 13 '22 at 08:50
  • @user17732522 Yes, even it is not clear to me what the author is trying to explain. Appreciate if you could shed some light on this. – Onkar N Mahajan Feb 13 '22 at 08:52
  • @OnkarNMahajan So are you just asking how to use conversion functions in general? – user17732522 Feb 13 '22 at 08:52
  • @user17732522, no, I just want to know what is happening here & how will operator Other be used and if someone can explain me in real world why would someone use some construct like this ? – Onkar N Mahajan Feb 13 '22 at 08:54
  • @康桓瑋, how will this work, can you shed some light ? – Onkar N Mahajan Feb 13 '22 at 08:55
  • 1
    @OnkarNMahajan https://stackoverflow.com/a/16615725/17732522? By the way the book you are referencing seems to be from 2000. If so, it is completely out-dated by now and I would strongly recommend to learn from a newer book, one that was published at least not before 2011 and which teaches at least C++11. (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user17732522 Feb 13 '22 at 08:59
  • please post a [mcve]. To answer this quesiton one would have to remove more from your code than one has to add. – 463035818_is_not_an_ai Feb 13 '22 at 09:00
  • @user17732522, agree, but in C++11 also this code would be same right ? – Onkar N Mahajan Feb 13 '22 at 09:03
  • 1
    This particular code in the book is just a test for various language features, and is not meant to have any meaning of its own. You don't normally want any implicit conversion operators at all. – n. m. could be an AI Feb 13 '22 at 09:19

1 Answers1

-1
Here’s an example of the functions that

are synthesized by the compiler:

// Functions that are synthesized by the compiler
#include <iostream>
using namespace std;
class GameBoard {
public:
 GameBoard() { cout << "GameBoard()\n"; }
 GameBoard(const GameBoard&) {
 cout << "GameBoard(const GameBoard&)\n";
 }
 GameBoard& operator=(const GameBoard&) {
 cout << "GameBoard::operator=()\n";
 return *this;
 }
 ~GameBoard() { cout << "~GameBoard()\n"; }
};
class Game {
 GameBoard gb; // Composition
public:
 // Default GameBoard constructor called:
 Game() { cout << "Game()\n"; }
 // You must explicitly call the GameBoard
 // copy-constructor or the default constructor
 // is automatically called instead:
 Game(const Game& g) : gb(g.gb) {
 cout << "Game(const Game&)\n";
 }
 Game(int) { cout << "Game(int)\n"; }
 Game& operator=(const Game& g) {
 // You must explicitly call the GameBoard
 // assignment operator or no assignment at
 // all happens for gb!
 gb = g.gb;
 cout << "Game::operator=()\n";
 return *this;
 }
 class Other {}; // Nested class
 // Automatic type conversion:
 operator Other() const {
 cout << "Game::operator Other()\n";
 return Other();
 }
 ~Game() { cout << "~Game()\n"; }
};
class Chess : public Game {};
void f(Game::Other) {}
class Checkers : public Game {
public:
 // Default base-class constructor called:
 Checkers() { cout << "Checkers()\n"; }
 // You must explicitly call the base-class
 // copy constructor or the default constructor
 // will be automatically called instead:
 Checkers(const Checkers& c) : Game(c) {
 cout << "Checkers(const Checkers& c)\n";
 }
 Checkers& operator=(const Checkers& c) {
 // You must explicitly call the base-class
 // version of operator=() or no base-class
 // assignment will happen:
Game::operator=(c);
 cout << "Checkers::operator=()\n";
 return *this;
 }
};
int main() {
 Chess d1; // Default constructor
 Chess d2(d1); // Copy-constructor
//! Chess d3(1); // Error: no int constructor
 d1 = d2; // Operator= synthesized
 f(d1); // Type-conversion IS inherited
 Game::Other go;
//! d1 = go; // Operator= not synthesized
 // for differing types
 Checkers c1, c2(c1);
 c1 = c2;
} 

The constructors and the operator= for GameBoardand Game announce themselves so you can see when they’re used by the compiler. In addition, the operator Other( performs automatic ) type conversion from a Game object to an object of the nested class Other. The class Chess simply inherits from Game and creates no functions (to see how the compiler responds). The function f( ) takes an Other object to test the automatic type conversion function. In main( ), the synthesized default constructor and copyconstructor for the derived class Chess are called. The Game versions of these constructors are called as part of the constructorcall hierarchy. Even though it looks like inheritance, new constructors are actually synthesized by the compiler. As you might expect, no constructors with arguments are automatically created because that’s too much for the compiler to intuit. The operator= is also synthesized as a new function in Chess using memberwise assignment (thus, the base-class version is called) because that function was not explicitly written in the new class.

  • 1
    please please, I can read that para (which you have copied here) from the book itself. Please don't do this .. remember there is no point in scoring point, it's worthless, we are all here to develop knowledge. – Onkar N Mahajan Feb 13 '22 at 14:26