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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:51:21+00:00 2026-06-04T21:51:21+00:00

I am practicing overloading operators in C++ right now and I have a problem.

  • 0

I am practicing overloading operators in C++ right now and I have a problem.
I created String class, it has just to fields one is char array other is length.
I have a String “Alice has a cat” and when I call

cout<<moj[2];

I would like to get ‘i’, but now I am getting moj + 16u adress of moj + 2 sizeof(String)
When I call

 cout<<(*moj)[2];

it works as it shoud but I would like to dereference it in overloaded operator definition. I tried many things but I can’t find solution. Please correct me.

char & operator[](int el) {return napis[el];}
const char & operator[](int el) const {return napis[el];}

AND the whole code, the important things are down the page. It’s compiling and working.

    #include <iostream>
   #include <cstdio>
   #include <stdio.h>
   #include <cstring>
  using namespace std;

 class String{
public:

//THIS IS  UNIMPORTANT------------------------------------------------------------------------------
char* napis;
int dlugosc;
   String(char* napis){
   this->napis = new char[20];
   //this->napis = napis;
   memcpy(this->napis,napis,12);
   this->dlugosc = this->length();
}

   String(const String& obiekt){
   int wrt = obiekt.dlugosc*sizeof(char);
   //cout<<"before memcpy"<<endl;
   this->napis = new char[wrt];
   memcpy(this->napis,obiekt.napis,wrt);

   //cout<<"after memcpy"<<endl;
   this->dlugosc = wrt/sizeof(char);
  }

   ~String(){
   delete[] this->napis;
   }

   int length(){
   int i = 0;
   while(napis[i] != '\0'){
       i++;
   }
   return i;
  }
        void show(){
      cout<<napis<<" dlugosc = "<<dlugosc<<endl;
 }


//THIS IS IMPORTANT
    char & operator[](int el) {return napis[el];}
    const char & operator[](int el) const {return napis[el];}
};


   int main()
   {

   String* moj = new String("Alice has a cat");
  cout<<(*moj)[2]; // IT WORKS BUI
 //  cout<<moj[2]; //I WOULD LIKE TO USE THIS ONE


   return 0;
   }
  • 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-06-04T21:51:23+00:00Added an answer on June 4, 2026 at 9:51 pm
    String* moj = new String("Alice has a cat");
    cout<<(*moj)[2]; // IT WORKS BUI
    //  cout<<moj[2]; //I WOULD LIKE TO USE THIS ONE
    

    That can’t be done, the subscript operator in the later case is applied to a pointer. It is only possible to overload operators when at least one of the arguments is of user defined type (or a reference to it, but not a pointer); in this particular case the arguments are String* and 2, both fundamental types.

    What you may do is drop the pointer altogether, I don’t see why you need it:

    String moj("Alice has a cat");
    // cout<<(*moj)[2]; <-- now this doesn't work
    cout<<moj[2]; // <-- but this does
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been practicing TDD and (some) XP for a few years now and
I have been practicing TDD in C# for several years now and recently have
I just started practicing TDD in my projects. I'm developing a project now using
I was practicing the dynamic programming problem on SPOJ. But I have no idea
I am practicing with functions on the vector class. i have written a function
I am practicing delegate in iphone development but have a problem 2012-05-12 21:21:54.279 DelegatePractice[2924:f803]
so im practicing the STL string class, but i can not figure out why
i'm practicing php at nowadays. i have one question. i see one = on
Im practicing with one way hash encryption. I have a MVC model with Entity
I have just started practicing OOP in php through the book Concepts, Techniques and

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.