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

  • Home
  • SEARCH
  • 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 407171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:34:06+00:00 2026-05-12T17:34:06+00:00

I should get this by now, but I’m just not getting it yet. The

  • 0

I should get this by now, but I’m just not getting it yet. The trouble is operator=’s argument could be non-const, but that breaks std::vector::push_back because it makes the item const, so operator= has to accept a const object. Well, I’m not certain on how I’m supposed to modify the this object working like this.

#include <vector>
#include <map>
#include <iostream>

using namespace std;

int font[] = {0, 31, 0, 31, 0, 31, 0, 31};

class Foo {
    int size_;
    std::map<int, int> chars_;
    public:
    Foo(int *font, int size);
    unsigned int Size() const { return size_; }
    void Add(int ch);
    bool operator==(const Foo &rhv) const;
    int &operator[](int i);
    int const operator[](int i);
    Foo operator=(const Foo &rhv);
};

Foo::Foo(int *font, int size) {
    for(int i = 0; i < size; i++ ) {
        chars_[size_++] = font[i];
    }
}

bool Foo::operator==(const Foo &rhv) const {
    if(Size() != rhv.Size()) return false;
    /*for(int i = 0; i < Size(); i++ ) {
        if ( chars_[i] != *rhv[i] ) 
            return false;
    }*/
    return true;
}

int &Foo::operator[](int i) {
    return chars_[i];
}

int const Foo::operator[](int i) {
    return chars_[i];
}

Foo Foo::operator=(const Foo &rhv) {
    if( this == &rhv ) return *this;
    for(unsigned int i = 0; i < rhv.Size(); i++ ) {
        //Add(*rhv[i]);
        //chars_[size_++] = rhv[i];
    }
    return *this;
}

void Foo::Add(int ch) {
    chars_[size_++] = ch;
}

int main()
{
    vector<Foo> baz;
    Foo bar = Foo(font, 8);
    baz.push_back(bar);    
}

Edit: Well, I’ve spent some time reading about const again. Is what I want to do even possible? The reason I ask is because of this sentence:
If it doesn’t compile without const qualifier and you are returning a reference or pointer to something that might be part of the object, then you have a bad design.

I took that into account, and refrained from returning a reference in the const method. That yielded this error:

test.cpp:18: error: 'const int Foo::operator[](int)' cannot be overloaded
test.cpp:17: error: with 'int& Foo::operator[](int)'
test.cpp:41: error: prototype for 'const int Foo::operator[](int)' does not match any in class 'Foo'
test.cpp:37: error: candidate is: int& Foo::operator[](int)

Getting rid of the int & Foo::operator[] gets rid of that error. I know I can just make a new accessor to apply changes to chars_, but I thought I’d update this and find out if what I’m trying to do is possible at all.

  • 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-12T17:34:06+00:00Added an answer on May 12, 2026 at 5:34 pm

    The first problem is the syntax of:

    int const operator[](int i);
    

    Should be:

    int operator[](int i) const;
    

    However, fixing that fails, because std::map has no const operator[]. Why is that one may ask? Because it has a side-effect:

    Returns a reference to the object that
    is associated with a particular key.
    If the map does not already contain
    such an object, operator[] inserts the
    default object data_type().
    …
    Since operator[] might insert a new
    element into the map, it can’t
    possibly be a const member function.
    Note that the definition of operator[]
    is extremely simple: m[k] is
    equivalent to
    (*((m.insert(value_type(k,
    data_type()))).first)).second.
    Strictly speaking, this member
    function is unnecessary: it exists
    only for convenience.

    Taken from http://www.sgi.com/tech/stl/Map.html. So instead of operator[] you will want to use the find function.

    By the way, for this kind of experimentation and study, I find it convenient to write all class functions inline in the class declaration. Simply, because it faster to modify the class definition, though some C++ purists describe this as bad style.

    [EDIT: Here’s a full solution]

    class Foo {
        size_t size_;
        std::map<int, int> chars_;
     public:
        Foo(int *font, size_t size) 
            : size_(size) 
        { 
            // size should be of type "size_t" for consistency with standard library
            // in the original example "unsigned int" and "int" was mixed throughout
            for (size_t i=0; i < size; ++i)
                // Reuse the add function.  
                Add(font[i]);
        }
        size_t Size() const { 
            return size_; 
        }
        void Add(int ch) { 
            chars_[size_++] = ch;
        }
        bool operator==(const Foo &rhv) const {
            if (&rhv == this) return true;
            if (rhv.Size() != size_) return false;
            for (size_t i=0; i < size_; ++i)
                if (rhv[i] != (*this)[i])
                    return false;
            return true;
        }   
        int& operator[](size_t i) {
            assert(i < size_);
            return chars_.find(i)->second;
        }
        const int& operator[](size_t i) const {
            assert(i < size_);
            return chars_.find(i)->second;
        }
        Foo& operator=(const Foo &rhv) {
            size_ = rhv.size_;
            chars_ = rhv.chars_;
            return *this;
        }
    };
    
    int main()
    {
        std::vector<Foo> baz;
        Foo bar = Foo(font, 8);
        baz.push_back(bar);    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have this GET request which works in Chrome but not in
I'm still not sure I totally get how this particular case should work out.
This should be totally simple but I can't get it working no matter what
This should be simple, yet I can't get it to work. I have a
This seems like it should be very simple but I can't get it to
This should be a simple question, but I can't get it to work :(
I just can't figure it out, why i get this error. It is not
I've been trying to get this to work for quite a while now but
I want to get a window like this How should i get this and
i get this error when trying this: ERROR method name expected. How should i

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.