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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:15:09+00:00 2026-06-17T09:15:09+00:00

#include <iostream> class Bar { protected: public: int & x; Bar(int & new_x) :x(new_x)

  • 0
#include <iostream>

class Bar
{
    protected:

public:
        int & x;
        Bar(int & new_x)
        :x(new_x)
        {}
        Bar & operator = (const Bar toCopy)
        {
            x = toCopy.x;
            return *this;
        }
};

int main()
{
    int x1(1);
    int x2(2);

    Bar bar = Bar(x1);
    std::cout << bar.x << std::endl;

    bar = Bar(x2);
    std::cout << bar.x << std::endl;

    bar.x = 5;
    std::cout << bar.x << std::endl;

    std::cout << x1 << std::endl;
    std::cout << x2 << std::endl;

}

The output is:

1
2
5
5
2

What I am trying to do is copy x and save it within the object bar.

The output suggests to me that the assignment operator has not done its magic, both in terms of copying and taking the value of the new object. I have followed this link.

Changing x into a value rather than a reference is not possible, as in the real program x is an abstract class.

Please if possible abstain from using heap assignment.

EDIT:
1. I realized that I have just butchered the C++ language. I would like to apologize to all “C++ian” speaking computers. My motivation was to assign a member abstract variable on the stack. As far as I understand, it cannot be done on the stack, because the size of the derived class is not known at compile time.
2. OK… I am a total n00b…(no sh*t, Sherlock!). “Effective C++ programming” @rhalbersma is a must have. It contains essentials, yet you wont find them anywhere (copy constructions, copy initializer),.. in one place anyway.

  • 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-17T09:15:09+00:00Added an answer on June 17, 2026 at 9:15 am

    References can be confusing. So can const pointers. I’ll try to clear things up by talking about both of them at the same time. What can I say, I’m an optimist.

    First, const pointers.

    We’ll start with a class called Foo. We can have a pointer to this class — a Foo*. We can have a pointer to a const instance of this class — a Foo const*. And we can have a const pointer to a non-const instance of this class — a Foo*const.

    A Foo const* is a pointer you can change to data you cannot change.

    A Foo*const is a pointer you cannot change to data you can change.

    I’ll assume you got that. I am, after all, an optimist. Next, lets look at references.

    While it is true that references are aliases, sometimes it helps to think about them in a world where things have concrete implementations in terms of other types.

    A Foo& is analogous to a Foo*const — a non-changeable “pointer” to an instance of Foo you can change. So the Foo you are talking about is always the same one, but you can change its state.

    Now, it is a bit more than that. There is some syntactic sugar. When you create a reference to another variable, it does the & automatically — so when you do a Foo& f = a;, this is analogous to Foo*const f = &a;.

    Second, when you use ., it does the same as -> in the pointer case. (And similarly for (most?) other operators)

    Third, when you do assignment, it clearly cannot change the pointer’s value — because it is const — but instead it changes the value of the thing pointed to. So Foo& f = a; a = b; does the equivalent of Foo*const f = &a; *f = b;.

    It assigns the thing pointed to, rather than the pointer, when you use operator=. But when you initialize it, it doesn’t use operator=, even if you have a = token.

    Initialization is not the same as assignment in general, and the semantics of what happens with a & reference and a * pointer in initialization and assignment are very different.

    Foo& f = a; f = b; does two completely different things. Initialization of a reference initializes the “const pointer” portion — assignment to a reference modifies the thing pointed to.

    My personal name for how initialization and assignment mean different things with references is “reference semantics”, as opposed to “pointer semantics” where both initialization and assignment change what thing is being pointed to. In “reference semantics”, initialization picks what thing is being pointed to, and assignment changes the state of the thing that is pointed to.

    This is highly confusing, and I hope I helped make it confusing in a different way.

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

Sidebar

Related Questions

My code: #include <iostream> using namespace std; class Foo { public: int bar; Foo()
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
#include<iostream.h> #include<conio.h> #include<string.h> class flight { private : int flightno; char source[30],destination[30]; protected :
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Possible Duplicate: Difference between const declarations in C++ #include <iostream> class Bar{}; void foo(const
#include <iostream> class Foo { }; Foo createFoo() { return Foo(); } void bar(Foo
Consider the following code: #include <iostream> class Bar { public: void foo(bool b =
Given this code: #include <iostream> using namespace std; class Foo { public: Foo ()

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.