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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:04:30+00:00 2026-05-18T11:04:30+00:00

I want to create a class in c++. This class must manage with a

  • 0

I want to create a class in c++. This class must manage with a collection.
OK, no problem, I would like to use operator[] of course but, in this case, my wish is to index not by position, but by name ==> that means using a string indexer.

It seems that something of this kind is not so nice to my compiler:

// In hpp
class myclass {
   ...
   ...
   std::string operator[](const std::string& name);
}
// In cpp
std::string myclass::operator[](const std::string& name) {
   ...
}
// In main
myclass m;
std::string value = m["Name"];

Compiler tells me that he cannot solve this because operator[const char[5]] does not exists.
OK OK
I could figure this…
Compiler thinks that by calling m[“Name”] I’m trying to call an operator admitting a char* and not a string… ok
Let’s change the code with operator[] allowing a char* as parameter… nothing.

Can somebody tell me how to achieve such a result in c++ in a best practice way? I suppose that is a common problem to index by string and not by integer…
Thank you.

  • 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-18T11:04:30+00:00Added an answer on May 18, 2026 at 11:04 am

    It should work fine. See this example which compiles and works ok for me:

    #include <iostream>
    #include <string>
    
    class MyClass
    {
        public:
            std::string operator[] (const std::string& key) { std::cout << key << std::endl; return key; }
    };
    
    int main()
    {
        MyClass obj;
        std::string s = obj["50"];
        std::cout << s << std::endl;
    }
    

    And I see no reason it should not, since std::string has implicit constructor taking const char* so the conversion should be automatic.

    Edit: From the comment it seems your problem was with your main beeing like this:

    int main()
    {
        MyClass obj();
        std::string s = obj["50"];
        std::cout << s << std::endl;
    }
    

    The reason:

    An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

    [ Note: since () is not permitted by the syntax for initializer,

    X a ();

    is not the declaration of an object of class X, but the declaration of a function taking no argument and returning an X.

    The form () is permitted in certain other initialization contexts (5.3.4, 5.2.3, 12.6.2). — end note ]

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

Sidebar

Related Questions

I want to extend RuntimeException to create this specific exception: class CompileLinkException extends RuntimeException
I want to create a class which is derived from QLineEdit ,but I can
I want to create whois class like that public class DomainInfo { public string
I'm really sorry. This must seem like an incredibly stupid question, but unless I
I want to create a class that exends Comparator. This comparator will compare two
I want to create a class to speed up things like getting application delegate,
I want to create a class to hold my specific variable like a bitmap.
I want to create a collection in VB.NET, but I only want it to
I feel like this must be simple to do but can't get it done
I want to create a class for storing attributes of the many data files

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.