Constructor Overloading in C++

前端 未结 5 2019
慢半拍i
慢半拍i 2020-12-13 18:28

My C++ overloading does not act as I assume it should:

#include \"Node.h\"
#include 

Node::Node()
{
    cout << \"1\" << endl;
          


        
5条回答
  •  [愿得一人]
    2020-12-13 19:19

    In a nutshell:

    #include 
    #include "Node.h"
    
    Node::Node()
        : game(Game()), value(0.), numVisits(0)
    {
        std::cout << "1" << std::endl;
    }
    
    Node::Node(double v)
        : game(Game()), value(v), numVisits(0)
    {
        std::cout << "2" << std::endl;
    }
    
    Node::Node(Game g, double v)
        : game(g), value(v), numVisits(0)
    {
        std::cout << "3" << std::endl;
    }
    

    As everyone said, you cannot call a constructor overload from a constructor. Delegation is an upcomming feature we'll meet with C++11. It's not much text to type, don't be lazy.

提交回复
热议问题