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

The Archive Base Latest Questions

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

Is the passing by reference of a private variable in a class to be

  • 0

Is the passing by reference of a private variable in a class to be directly changed outside that class acceptable practice? Or is this something that the compiler ‘should’ pick up and prevent?

Example:

//-------------------------------------------
class Others
{
public:
 Others() {};
 void ChangeIt(string &str) { str = "Changed by Others"; }
};

//-------------------------------------------
class Locals
{
private:
 string PrivateString;
public:
 Locals() { PrivateString = "Set by Locals"; };
 void VisitOthers() { Others o; o.ChangeIt(PrivateString); }
 const string GetString() { return PrivateString; }
};

//-------------------------------------------
int main(void)
{
 Locals lo;
 cout << lo.GetString() << "\n";
 lo.VisitOthers();
 cout << lo.GetString() << "\n";
 return 0;
}

Output:

Set by Locals
Changed by Others

I need to do something like this using other/different objects, private to the owner class, but changeable by others when needed. Last thing I want is for this kind of practice to come back & byte me in the future.

What is essentially worrying me, is that I would like to view the class/struct as basically a pointer to a buffer, and the member’s address as offsets into this buffer, so that even if you pass the pointer-value of a member it would be useless without the base-pointer of the class/struct to which it belongs. This is what I instinctively feel should be the case, so that the above example should not even be possible.

  • 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-16T22:41:28+00:00Added an answer on May 16, 2026 at 10:41 pm

    There is nothing to prevent, you pass your private member by reference. The function you are calling isn’t accessing your private member, it is changing it’s own argument (that happens to be the member of some class). The code is OK, but the important thing is that the function you called doesn’t keep a reference to your private member.

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

Sidebar

Related Questions

I'm passing a reference of a form to a class. Within this class I
I noticed that when passing reference parameters to boost bind, those parameters won't act
What's the reason of passing a value for a self reference in class functions
What are the benefits of passing by pointer over passing by reference in C++?
Trying to use an excpetion class which could provide location reference for XML parsing,
If I am passing an object to a method, why should I use the
I have a confusion that when i pass a variable by refrence in the
Possible Duplicate: Is Java pass-by-reference? I found an unusual Java method today: private void
Passing an undimensioned array to the VB6's Ubound function will cause an error, so
I'm passing raw HTTP requests to an apache server (received by PHP). The request

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.