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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:35:40+00:00 2026-05-30T20:35:40+00:00

I am wondering if there is a way to overload operator[] for a non-class

  • 0

I am wondering if there is a way to overload operator[] for a non-class type in C++.

Basically, there is a data type which is a pointer (CFDictionaryRef from CoreFoundation). But it’s not a class (i know that overloading operator[] for a specific class is allowed). I know how to access each element inside the CFDictionaryRef (for example, by using CFDictionaryGetIndex(CFIndex index); ). I want to make it simplified so that I don’t have to write that function call every time. I want to overload the operator[ ] for CFDictionaryRef. But since it’s not a class, from what I see, it’s not possible.

Anyone got any suggestions?

  • 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-30T20:35:41+00:00Added an answer on May 30, 2026 at 8:35 pm

    You’re right, it’s not possible to overload operators on non-user-defined types.

    What you might do is wrap the pointer in a class and overload the operators on the class itself. Since you can only overload operators on class types, this is the only option.

    class CFDictionaryRefWrapper {
    public:
        CFDictionaryRefWrapper(CFDictionaryRef r) : dref(r) { }
    
        CFDictionaryRef dref;
    
        Type operator[](unsigned int index) {
            /* do whatever with dref */
        }
    };
    

    This also has the advantage of being able to automatically manage the lifetime of the pointer (RAII) if you need to do that.

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

Sidebar

Related Questions

I am wondering is there any way to execute following shell script, which waits
Wondering if there is any way to get the lambda expressions that result from
I wondering is there any way to set restrictions on template class? Specify that
I am wondering: is there a way to call a method from within itself
just wondering is there any way that I can return one value from my
I was wondering is there any way to load geometry from those export files
I'm wondering is there a way to overwrite Hashtable (or Dictionary) class so it
just wondering is there a way to List all objects in database which have
I'm wondering is there a way to retrieve the type hinting associated with attributes
I was wondering is there a way to escape all html tags from a

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.