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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:54:50+00:00 2026-05-13T14:54:50+00:00

Problem with operator[] in c++, i have some class: 197 class Permutation{ 198 private:

  • 0

Problem with operator[] in c++, i have some class:

197 class Permutation{
198         private:
199                 unsigned int* array;
200                 unsigned int size;
201
202                 void fill(){
203                         for(unsigned int i=0;i<size;i++)
204                                 array[i]=i;
205                 }
206                 void init(const unsigned int s){
207                         if(s){
208                                 array=new unsigned int[s];
209                                 size=s;
210                         }else{
211                                 size=0;
212                                 array=0;
213                         }
214                 }
215                 void clear(){
216                         if(array){
217                                 delete[]array;
218                                 array=0;
219                         }
220                         size=0;
221                 }
222         public:
223                 Permutation(const unsigned int& s=0):array(0),size(0){
224                         init(s);
225                         fill();
226                 }
227                 ~Permutation(){
228                         clear();
229                 }
230                 unsigned int& operator[](const unsigned int& idx){
231                         assert(idx<size);
232                         return array[idx];
233                 }
234                 unsigned int& get(const unsigned int& idx)
235                 {
236                         assert(idx<size);
237                         return array[idx];
238                 }


253                 Permutation& operator=(const Permutation& p){
254                         clear();
255                         init(p.size);
256                         size=p.size;
257                         for(unsigned int i=0;i<size;i++)
258                                 array[i]=p.array[i];
259                         return *this;
260                 }
261
262                 Permutation(const Permutation&p)
263                 {
264                         clear();
265                         init(p.size);
266                         size=p.size;
267                         for(unsigned int i=0;i<size;i++)
268                                 array[i]=p.array[i];
269                 }
};

when I use

Permutation x(3);
x[0]=1;

it works very well, but when I use:

Permutation* x=new Permutation(3);
x->get(0)=10; // this works fine
x[0]=1;

in this case, in debugger I see it is called a constructor of new object for Permutation class, what is going on ? and why?
I someone know what is going about I would appreciate for information.

  • 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-13T14:54:51+00:00Added an answer on May 13, 2026 at 2:54 pm

    First, your code:

    Permutation* x=new Permutation(3);
    x->get(0)=10; // this works fine
    

    And then you do this:

    x[0]=1;
    

    And what you are doing is treating the pointer x as an array, and initializing it, which is longhand for:

    x[0] = Permuation(1);  // implicit conversion using Permulation(const unsigned long&)
    

    What you meant to write was:

    (*x)[0]=1;  // follow x and then invoke the [] operator
    

    Or, equivalent:

    x->operator[](0) = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 326k
  • Answers 326k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have deployed a MVC 2 application and it works… May 14, 2026 at 1:38 am
  • Editorial Team
    Editorial Team added an answer You need to implement a custom designer (ControlDesigner) for your… May 14, 2026 at 1:38 am
  • Editorial Team
    Editorial Team added an answer No, a screenshot is exactly what it sounds like. You'll… May 14, 2026 at 1:38 am

Related Questions

I want to convert function object to function. I wrote this code, but it
I'm using two different libraries in my project, and both of them supply a
When I started as at my first job as software developer I was assigned
I'm looking for a C++ container that's a cross between boost::array, boost::scoped_array and std::vector.
I am writing a library in C++ and have some functions that work with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.