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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:41:17+00:00 2026-05-16T03:41:17+00:00

Would it be possible for a public function to return a pointer to a

  • 0

Would it be possible for a public function to return a pointer to a private variable in the class. If so / if not, what would happen? would it crash or is there anything highly unsafe about this? Can the pointed data be read or written to?
Thanks

  • 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-16T03:41:17+00:00Added an answer on May 16, 2026 at 3:41 am

    Yes, a member function may return a pointer (or reference) to a private data member. There is nothing wrong with this except that in most circumstances it breaks encapsulation.

    The data member can certainly be read via the returned pointer or reference. Whether or not it can be written to depends on whether the returned pointer or reference is to a const-qualified object (i.e., if you return a const T*, you won’t be able to modify the pointed-to T). For example:

    class Example
    {
    public:
        int*       get()             { return &i; }
        const int* get_const() const { return &i; }
    private:
        int i;
    };
    
    int main()
    {
        Example e;
    
        int* p = e.get();
        int a = *p;    // yes, we can read the data via the pointer
        *p = 42;       // yes, we can modify the data via the pointer
    
        const int* cp = e.get_const();
        int b = *cp;   // yes, we can read the data via the pointer
        *cp = 42;      // error:  pointer is to a const int         
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have simple class like: class MyClass { private $_prop; public function getProp()
Would it be possible to add a new operator to the String class that
I want to create a simple class in Javascript with only one public function
Public Function Foo() as String() Dim bar As String = {bar1,bar2,bar3} Return bar End
Possible Duplicate: C++ member-function pointer How to invoke pointer to member function when it's
i would like to know if it is possible to have a function in
following piece of code: class a{ public function __get($key) { if($key == 'obj') {
Class: Class: private: ... vector<string> words; vector< list<int> > vints; public: myFunction(...) I am
Would anyone happen to know if it's possible to set a namespace on a
I have a class that I would like to return true when method_exists() etc

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.