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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:10:30+00:00 2026-06-17T07:10:30+00:00

Today i’ve see very strange thing while working with references. Just one simple example:

  • 0

Today i’ve see very strange thing while working with references.

Just one simple example:

#include <iostream>

struct Base {
  enum Type {
    FOO = 0,
    BAR = 1
  };
  virtual ~Base() {}
  virtual Type type() const = 0;
  int value_;
};

struct Foo : Base { 
    Foo() { value_ = 33; }
    virtual Type type() const { return FOO; }
};

struct Bar : Base { 
    Bar() { value_ = 44; }
    virtual Type type() const { return BAR; }
};

int main() {
    Foo foo;
    Bar bar;
    Base & b = foo;
    std::cout << b.type() << ", " << b.value_ << "\n";
    b = bar;
    std::cout << b.type() << ", " << b.value_ << "\n";
    return 0;
}

What did you think output would be? I was really surprised when see it:

0, 33
0, 44

Tested on VS 2010, mingw 4.6, gcc 4.3. So, may be known secret of this magic?

Ideone link example

  • 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-06-17T07:10:31+00:00Added an answer on June 17, 2026 at 7:10 am

    References are like pointers in C++, with two important exceptions (aside from syntax):

    • Their cannot be assigned to null
    • They cannot be reassigned

    So, when you call b = bar, you are not reassigning the reference; you are assigning the value of bar to the object referenced by b; in this case, you are assigning the value of bar to foo. So, in the second line, you will have a Foo object with a value_ of 44. Just what your output says.

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

Sidebar

Related Questions

Today I experienced a very strange behavior with my XCode (using Version 4.2, iOS
Today I got into a very interesting conversation with a coworker, of which one
today i hit a really annoing problem with wpf. i just want to align
Today I started working on my first Ruby project. The goal is to convert
Today I ran into a very difficult TDD problem. I need to interact with
Today while inside a client's production system, I found a SQL Server query that
Today's problem in my code is kind of strange, and I could not reproduce
Today I had a strange encounter with gcc. consider the following code: float len[ELEM+1];
Today while investigating how/where to download and save audio/image files from a URL in
Today I just had my first class on Java ee and dynamic web project...

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.