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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:04:23+00:00 2026-05-23T16:04:23+00:00

Having spent the last couple of days experimenting with C++ operator[] methods, I’ve come

  • 0

Having spent the last couple of days experimenting with C++ operator[] methods, I’ve come across an anomaly that I can’t easily explain. The code below implements a simple test string class, which allows the user to access single characters via its subscript “operator” method. As I would like to differentiate between lvalue and rvalue subscript contexts, the “operator” method returns an instance of the custom “CReference” class as opposed to a standard C++ character reference. Whilst the “CReference” “operator char()” and “operator=(char)” methods appear to handle each rvalue and lvalue context respectively, g++ refuses to compile without the presence of an additional “operator=(CReference&)” method as documented below. Whilst the addition of this method appears to placate some kind of compile-time dependency, it is never actually invoked at run-time during the execution of the program.

As someone who thought they had acquired a fundamental understanding of C++ intricacies, this project has certainly proved to be a humbling experience. If anyone could see their way to enlightening me as to what’s going on here, I would be eternally grateful. In the meantime, I’m going to have to pull out the C++ books in order to reconcile the void** between what I know and what I think know.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// Class for holding a reference to type char, retuned by the "operator[](int)"
// method of class "TestString". Independent methods permit differentiation
// between lvalue and rvalue contexts.
class CReference
{
 private:
   char& m_characterReference;

 public:
   // Construct CReference from char&
   CReference(char& m_initialiser)
     : m_characterReference(m_initialiser) {}

   // Invoked when object is referenced in a rvalue char context.
   operator char()
   {
    return m_characterReference;
   }

   // Invoked when object is referenced in a lvalue char= context.
   char operator=(char c)
   {
    m_characterReference = c;
    return c;
   }

   // NEVER INVOKED, but will not compile without! WHY???
   void operator=(CReference &p_assignator){}
};


// Simple string class which permits the manipulation of single characters
// via its "operator[](int)" method.
class TestString
{
 private:
   char m_content[23];

 public:
   // Construct string with test content.
   TestString()
   {
    strcpy(m_content, "This is a test object.");
   }

   // Return pointer to content.
   operator const char*()
   {
    m_content[22] = 0;
    return m_content;
   }

   // Return reference to indexed character.
   CReference operator[](int index)
   {
    return m_content[index];
   }
};


int main(int argc, char *argv[])
{
 TestString s1;

 // Test both lvalue and rvalue subscript access.
 s1[0] = s1[1]; 

 // Print test string.
 printf("%s\n", (const char*)s1);
 return 0;
}
  • 1 1 Answer
  • 2 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-23T16:04:24+00:00Added an answer on May 23, 2026 at 4:04 pm
    1. The line s1[0] = s1[1]; causes the compiler to generate an implicit copy assignment operator for CReference if you didn’t declare one yourself. This causes an error because your class has a reference member, which can’t be copied.

    2. If you added an assignment operator that takes a parameter of type const CReference&, it would get called by the assignment.

    3. In your code, you declared a copy assignment operator of type void operator=(CReference &p_assignator). This can’t be called because the righthand side of the assignment is a temporary object, which can’t be bound to a non-const reference. However, the act of declaring this operator causes the compiler not to try to define an implicit copy assignment operator, and therefore avoids the previous compilation error. Since this operator can’t be called, the compiler goes for the other assignment operator that takes a parameter of type char.

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

Sidebar

Related Questions

I have spent the last couple of days having this issue. I have read
I've spent the last couple of days getting to grips with the basics of
I've been having problems with this code I had spent the last 3 hours
I've spent a few days searching a for solution to my problem. I'm having
I've been having a glitch in my program for the last couple of hours
I have spent the last couple of hours trying to get my sessions to
New to MVC3 Razor - linq to sql having spent some time trying to
Spent some time troubleshooting a problem whereby a PHP/MySQL web application was having problems
I am having trouble getting the JSF annotations to work. I have spent some
Having spent a small age looking for the solution and having now found it,

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.