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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:18:17+00:00 2026-05-21T07:18:17+00:00

Sorry if this has been asked before, but I can’t find a clear enough

  • 0

Sorry if this has been asked before, but I can’t find a clear enough answer.

What is the correct way to provide public read-only access to an array member in C++?

if i have a class like the following:

   
class Particle
{
    double _position[10];

public:
    double* get_position()
    {
       return _position;
    }
};
   

I guess is really bad to return a pointer to the array, since that means it can be changed at
any time outside of the class, is it enough to return a const pointer instead?

I have seen other questions about the use of arrays in C++, and how is a better option to use vectors, nevertheless I’m really curious about this issue.

As you can see I’m only a C++ noob, sorry if this is a stupid question.

P.D. Sorry for my bad English.

  • 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-21T07:18:18+00:00Added an answer on May 21, 2026 at 7:18 am

    You can return a const pointer:

    class Particle
    {
        double _position[10];
    
    public:
        const double* get_position() const
        {
           return _position;
        }
    };
    

    Note that I’ve also made the member-function const (the second const), to tell the compiler that this member-function may be called on const Particle instances.

    Note: as you’ve already mentioned, a better solution is to use STL vectors and so on…

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

Sidebar

Related Questions

Sorry if this has been asked before, I did check but couldn't find anything...
This question has been asked before but 1) the user never accepted an answer
I've tried searching through search engines,MSDN,etc. but can't anything. Sorry if this has been
(I’m sorry if this has been asked before; the search feature seems to be
First of all, sorry if this has been asked before. I've done a pretty
I know this has been asked 100 times before but every bit of code
Sorry if this question has been asked before. On my search through SO I
Ok, I know this has been asked a thousand times before, but no conclusive
So, if this question has been asked before, I'm sorry. I'm not exactly sure
Sorry if this has been answered before, but all the related questions didn't quite

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.