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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:32:00+00:00 2026-05-12T21:32:00+00:00

In C++ , why is the following element access in a vector invalid? void

  • 0

In C++, why is the following element access in a vector invalid?

void foo(std::vector<int>* vecPtr) {
  int n = vecPtr->size(); // ok
  int a = vecPtr->[0];    // invalid
}

Instead, we have to write the more cumbersome

  (*vecPtr)[0] = 1;

I think, the operator[] call should just have the same syntax like a method call, and I hate the extra star and parentheses. (I know C++ has a lot more serious issues, but this one annoys me every time when I have to type it …)

  • 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-12T21:32:00+00:00Added an answer on May 12, 2026 at 9:32 pm

    It’s because the language expects a member to appear after ->. That’s how the language is made up. You can use the function call syntax, if you like

    // not really nicer
    vecPtr->operator[](0);
    

    If you have to do this a lot in sequence, using [0] instead of the parentheses can improve readability greatly

    vecPtr[0][0]
    

    Otherwise, for one level, i find (*vecPtr)[0] is perfectly readable to me.

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

Sidebar

Related Questions

I have the following element which specifies the size=15. However the rendered element, which
Consider the following code: typedef std::vector<int> cont_t; // Any container with RA-iterators typedef cont_t::const_iterator
Suppose i have a std::vector<int> v //and ... for(int i =0;i<100;++i) v.push_back(i); now i
I have an iframe and in order to access parent element I implemented following
I need to access a vector pointer elements, I have the following code for
i have a std::vector<int> and a second container holding iterators or indexes (no keys,
I have a window with the following elements, and I'm trying to access the
I have the following element on my page, which gets a dynamically added ID
I have the following element selected: >>> order <Element Order at 0x10364b960> To get
Say I have the following element: <p id=thingy>Here is some <em>emphasized</em> text!</p> In 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.