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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:10:29+00:00 2026-05-27T16:10:29+00:00

In some C++ code, I am encountering a compile-time error that seems like it

  • 0

In some C++ code, I am encountering a compile-time error that seems like it might be indicating that I am trying to reseat a reference. But I feel quite sure that I am not trying to reseat a reference. I do not understand the cause of the error. Perhaps I am being silly, and missing something obvious. Or perhaps the problem reflects deep principles of C++ template programming that I do not adequately understand. Either way, I hope some of you can help. This is the code:

// p.cpp - slimmed down demonstration of error in prop.cpp

template<class T>
class Property {
protected:
    T& value;

public:
    explicit Property(T& a) : value(a) { }

    // default "copy" setter
    virtual Property<T>& operator=(T a) { value = a; return *this; }

    // default "copy" getter
    virtual operator T() const { return value; }

    template<class U> // must invoke U virtual getter and T virtual setter
    Property<T>& operator=(const Property<U>& newval)
        { return (*this = U(newval)); }

    /* // uncommenting this eliminates the error
    Property<T>& operator=(const Property<T>& newval)
        { return (*this = T(newval)); }
    /**/
};

int main()
{
    //* // this code produces the error
    {
        int i_ = 10, j_;
        Property<int> i (i_), j (j_);
        j = i;
    }
    /**/

    /* // this code does NOT produce the error
    {
        int i_ = 10;
        long j_;
        Property<int> i (i_);
        Property<long> j (j_);
        j = i;
    }
    /**/

    return 0;
}

When I compile this with gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 using the command g++ -o p p.cpp I get this output:

p.cpp: In member function ‘Property<int>& Property<int>::operator=(const Property<int>&)’:
p.cpp:4:7: error: non-static reference member ‘int& Property<int>::value’, can’t use default assignment operator
p.cpp: In function ‘int main()’:
p.cpp:33:13: note: synthesized method ‘Property<int>& Property<int>::operator=(const Property<int>&)’ first required here

Before you tell me that I am trying to reseat a reference, please keep in mind that:

  1. Uncommenting the definition for Property::operator= that explicitly takes the same constructed type on the right side as the constructed type on the left side eliminates the error. So the problem is not, per se, that Property<int>::value is being accessed through *this.

  2. Independently, as shown in main(), the operator= method template inside the Property class template is instantiated and works fine when the data type of the value on the right side of = is different from the data type on the left side of =.

  3. value is a reference, but there is nothing wrong with assigning a value to it with =, at least in general. It is non-const. So once it is instantiated (which is guaranteed, via the initialization list in Property‘s only constructor), assigning a value to it does not attempt to reseat it, but instead assigns a new value to the memory location to which it refers.

I will provide the larger file prop.cpp which contains (successful) unit tests for each of Property‘s member functions, if requested.

You may notice that this code is an attempt to (partially) implement “function-like” (“C#-style”) properties in C++. But this question is not about whether or not it would actually be appropriate to do this a real-world C++ project, nor is it about whether or not the pattern I have chosen is the most appropriate. It’s possible that I’ll ask separate questions about that later. If you want to comment on or criticize my approach, I am interested. I only ask that you not do it here, because it would distract from the more specific purpose of this question. Instead, you could post your own question, possibly with your own answer, and post a brief comment here linking to your new question.

  • 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-27T16:10:30+00:00Added an answer on May 27, 2026 at 4:10 pm

    As the compiler said, non-static reference cannot be assigned using the compiler-generated assignment operator. A user-defined copy assignment operator is required.

    Your user-defined assignment operator performs assignment to the object referenced by value, by calling first your operator T() to obtain a temporary value of type T and then your Property<T>& operator=(T a) to execute value = a, which replaces the referenced-to object with the value obtained from the conversion operator.

    If nothing is derived from your class, your operator= is equivalent to

    // uncommenting this eliminates the error
    Property<T>& operator=(const Property<T>& newval)
        {
    //          return (*this = T(newval)); // convert to T, then assign
                value = newval.value;       // assign the referenced object
                return *this;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m encountering this problem trying to mock some objects that receive complex lambda expressions
I'm encountering this error while compiling some old VC++ 6.0 source code. error C2632:
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
Take some code like if (person.IsMale()) { doGuyStuff(); } else { doGirlStuff(); } Should
I've been encountering some strange behavior when trying to find a key inside a
Some code I have requires the JCE unlimited Strength Policy Files. I'd like to
I am encountering some performance problems with my Entity Framework Code-First queries and I
Ok, this is a curly one. I'm working on some Delphi code that I
We have taken over some .NET 1.1 Windows Service code that spawns threads to
Some consumers of our WCF web service are encountering an exception when trying to

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.