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 7398645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:53:46+00:00 2026-05-29T03:53:46+00:00

what is wrong with this code, why do I get wrong answer: class X

  • 0

what is wrong with this code, why do I get wrong answer:

class X
{
private:
        const int a;
        const int& b;
public:
        X(): a(10) , b(20)
        {
        //      std::cout << "constructor : a " << a << std::endl;
        //      std::cout << "constructor : b " << b << std::endl;
        }

        void display()
        {
            std::cout << "display():a:" << a << std::endl;
            std::cout << "display():b:" << b << std::endl;

        }
};


int
main(void)
{
        X x;
        x.display();
return 0;
}

The above code will give me the result as

display():a:10
display():b:1104441332

But If I remove the commented 2 lines inside the default constructor it gives me proper result which is

constructor : a 10
constructor : b 20
display():a:10
display():b:20

please help, Thank you

  • 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-29T03:53:46+00:00Added an answer on May 29, 2026 at 3:53 am

    You are initializing b as a reference to a temporary.

    The value 20 is created and exists only for the scope of the constructor.

    The behavior of the code after this is very interesting – on my machine, I get different values from the ones you posted, but the fundamental behavior is still nondeterministic.

    This is because when the value to which the reference points falls out of scope, it begins to reference garbage memory instead, giving unpredictable behavior.

    See Does a const reference prolong the life of a temporary?; the answer https://stackoverflow.com/a/2784304/383402 links to the relevant section of the C++ standard, specifically the below text:

    A temporary bound to a reference member in a constructor’s ctor-initializer
    (12.6.2) persists until the constructor exits.
    

    This is why you always get the right value in the print within the constructor, and rarely (but possibly sometimes!) after. When the constructor exits, the reference dangles and all bets are off.

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

Sidebar

Related Questions

Given this code: #include <iostream> using namespace std; class Foo { public: Foo ()
Can anyone tell me what's wrong with this code? class Dataset < ActiveRecord::Base has_many
Whats wrong with this code? -(void) drawRect:(CGRect) rect { CGContextRef c = UIGraphicsGetCurrentContext(); if
Why is this code correct: try { } catch(ArrayOutOfBoundsException e) {} and this wrong:
What is wrong with this code. The code is finding the javascript and debug1
Something must be wrong with this code right here: + (UIImage*)captureView:(UIView *)theView { UIGraphicsBeginImageContext(theView.frame.size);
What is wrong with this code ? set<string> nk ; bitset<3> bs1(string(100)); nk.insert(bs1.to_string()); error:
What's wrong with this C# code? I tried to overload the + operator to
What am I doing wrong in this HTML code: <html> <head> </head> <body> <div
What's wrong with this snippet of code? import numpy as np from scipy import

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.