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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:24:23+00:00 2026-05-25T22:24:23+00:00

Concisely, my goal is to have foo[bar] return type1, and foo[bar]= return type2. I

  • 0

Concisely, my goal is to have foo[bar] return type1, and foo[bar]= return type2.

I am writing an object in C++, and it’s coming along quite nicely, however there’s just one tiny little thing that I wish to do, yet it seems impossible.

My object is a storage class, so I am using an array subscript to access values. I also need to assign, so I also overload the = operator. However, it is somewhat inconvenient because the values that my class holds are first class objects, so for my array subscript overload I can’t return them as is. I have to return an intermediate class to handle the = operator, but I also want to retrieve the value without additional typing.

Is there any way to do this? Hackish ways are acceptable.

Edit: Here’s an example of what it (should) do

#include<cstdio>
#include<cstdlib>

class foo{
    char* a[100];
    foo(){
        for( int i = 0; i < 100; i ++)
            a[i] = 0;
    }
    char* operator[] (int location){
        return a[location];
    }
    foo& operator[]= (int location, const char* value ){
        if( a[location] == 0 )
            a[location] = (char*) malloc( strlen( value ) + 1 );
        else
            a[location] = (char*) realloc( a[location], strlen( value ) + 1 );
        strcpy( a[location], value );
    }
};
int main(){
    foo bar;
    bar[20] = "Hello";
    printf( "bar[20] = %s\n", bar[20] );
    bar[20] = "Hello There";
    printf( "bar[20] = %s\n", bar[20] );
    printf( "bar[20][0] = %c\n", bar[20][0] );
}

Output:
bar[20] = Hello
bar[20] = Hello There
bar[20][0] = H

Edit again: I think I’ll try phrasing this in a different, but workable way. Is there a way to overload the return type when a class is referenced? Such that if I have

class foo{
    bool a;
    bool operator /*referenced*/ (){
        return a
    }
    foo& operator=(bool b){
        a = b;
    }
};
int main(){
    foo a;
    a = b;
    if( a == b )
        printf("It Works!");
}

that would actually work?

  • 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-25T22:24:24+00:00Added an answer on May 25, 2026 at 10:24 pm

    There is no operator[]=, so the solution is to write some sort of wrapper class with two key features: an operator= that takes a value and sets it to the parent container, and an implicit conversion operator that takes the value from the parent container and returns it. Your operator[] will then return such wrapper.

    class foo
    {
        friend class wrapper;
    public:
        class wrapper
        {
            friend class foo;
            foo & _parent;
            int _index;
    
            wrapper(foo & parent, int index) : _index(index), _parent(parent) {}
        public:  
            wrapper & operator=(const char * value)
            {
                if( _parent.a[_index] == 0 )
                    _parent.a[_index] = (char*) malloc( strlen( value ) + 1 );
                else
                    _parent.a[_index] = (char*) realloc( _parent.a[_index], strlen( value ) + 1 );
                strcpy( _parent.a[_index], value );
    
                return *this;
            }
    
            operator char *()
            {
                return _parent.a[_index];
            }
        };
    
        char* a[100];
        foo()
        {
            for( int i = 0; i < 100; i ++)
                a[i] = 0;
        }
        wrapper operator[] (int location){
            return wrapper(*this, location);
        }
    };
    

    For the second question, well, you could always overload operator== on foo. But maybe I misunderstood.

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

Sidebar

Related Questions

I'm not sure quite how to phrase the question concisely, so if there is
I have code like the following: class SomeSharedData(object): def __init__(self): self._lock = RLock() self._errors
To describe my questions concisely, please have a look at examples below: Module os
When writing a mathematical proof, one goal is to continue compressing the proof. The
Not sure how to word the question concisely :). I have say 20 Posts
Does anyone know a good resource to concisely explain the different types of lists
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
I love list comprehensions in Python, because they concisely represent a transformation of a
This is a contrived example, but it illustrates my problem much more concisely then
I've found a pattern in my Views like this: <% if (someCondition) { Response.Write(string.Format(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.