Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:20:49+00:00 2026-05-25T10:20:49+00:00

My C++ overloading does not act as I assume it should: #include Node.h #include

  • 0

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

#include "Node.h"
#include <iostream>

Node::Node()
{
    cout << "1" << endl;
    Node(Game(), 0.0);
}

Node::Node(double v)
{
    cout << "2" << endl;
    Node(Game(),v);
}

Node::Node(Game g, double v)
{
    cout << "3" << endl;
    numVisits = 0;
    value = v;
    game = g;
}

And the output from:

Node n(16);
cout << n.value << endl;

is 0, when it should be 16.

What am I doing incorrectly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:20:50+00:00Added an answer on May 25, 2026 at 10:20 am

    Node(Game(),v); in your constructor doesn’t do what you expected. It just creates a temporary without using it and makes no effect. Then it immediately destructs that temporary when control flows over the ;.

    The correct way is initializing the members in each constructor. You could extract their common code in a private init() member function and call it in each constructor like the following:

    class Foo {
        public:
            Foo(char x);
            Foo(char x, int y);
            ...
        private:
            void init(char x, int y);
    };
    
    Foo::Foo(char x)
    {
        init(x, int(x) + 3);
        ...
    }
    
    Foo::Foo(char x, int y)
    {
        init(x, y);
        ...
    }
    
    void Foo::init(char x, int y)
    {
        ...
    } 
    

    C++11 will allow constructors to call other peer constructors (known as delegation), however, most compilers haven’t supported that yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java does not have concept of operator overloading. Still + operator behaves as addition
As far as my knowledge, Objective-C does not support method overloading. What can be
I'm defining an iterator type that does not store its current value explicitly. Instead,
C does not support function overloading. How can we then have 3 prototypes for
Is it the same as overloading, if not, can you please provide and example
C++ does not allow polymorphism for methods based on their return type. However, when
Possible Duplicate: Java - why no return type based method overloading? The compiler does
I want to build a webservice with this signature, which does not throw an
Possible Duplicate: Method overloading in Objective-C? Is method overloading not possible. I have two
I wondering why the generic overloading is not allowed, and it results in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.