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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:45:46+00:00 2026-05-20T07:45:46+00:00

Im trying to overload the [] operator in c++ so that I can assign

  • 0

Im trying to overload the [] operator in c++ so that I can assign / get values from my data structure like a dictionary is used in c#:

Array[“myString”] = etc.

Is this possible in c++?

I attempted to overload the operator but it doesnt seem to work,

Record& MyDictionary::operator[] (string& _Key)
{
for (int i = 0; i < used; ++i)
{
    if (Records[i].Key == _Key)
    {
        return Records[i]; 
    }
}
 }

Thanks.

  • 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-20T07:45:47+00:00Added an answer on May 20, 2026 at 7:45 am

    Your code is on the right track – you’ve got the right function signature – but your logic is a bit flawed. In particular, suppose that you go through this loop without finding the key you’re looking for:

    for (int i = 0; i < used; ++i)
    {
        if (Records[i].Key == _Key)
        {
            return Records[i]; 
        }
    }
    

    If this happens, your function doesn’t return a value, which leads to undefined behavior. Since it’s returning a reference, this is probably going to cause a nasty crash the second that you try using the reference.

    To fix this, you’ll need to add some behavior to ensure that you don’t fall off of the end of the function. One option would be to add the key to the table, then to return a reference to that new table entry. This is the behavior of the STL std::map class’s operator[] function. Another would be to throw an exception saying that the key wasn’t there, which does have the drawback of being a bit counterintuitive.

    On a totally unrelated note, I should point out that technically speaking, you should not name the parameter to this function _Key. The C++ standard says that any identifier name that starts with two underscores (i.e. __myFunction), or a single underscore followed by a capital letter (as in your _Key example) is reserved by the implementation for whatever purposes they might deem necessary. They could #define the identifier to something nonsensical, or have it map to some compiler intrinsic. This could potentially cause your program to stop compiling if you move from one platform to another. To fix this, either make the K lower-case (_key), or remove the underscore entirely (Key).

    Hope this helps!

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

Sidebar

Related Questions

I am trying to overload my operators its really just a class that holds
From birth I've always been taught to avoid nested arrays like the plague for
I am trying to basically do 3 things at once here: overload the assignment
I am trying to create a value template class, where additional properties can be
Trying to figure out out how to overload parenthesis on a class. I have
I'm trying to accomplish namespace NTL { typedef std::valarray vector; } through standard C++.
Okay, not sure what I'm doing here, other than it's not right. Trying to
Just finding my way around templates so was trying out a few stuff. Let
Is it possible to overload how a type resolves properties when cast to dynamic?
My program compiles fine, but gets core dumped when an overloaded function is called.

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.