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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:18:20+00:00 2026-05-31T06:18:20+00:00

As the title says: Is it good practice to create functions to access class

  • 0

As the title says: Is it good practice to create functions to access class variables?

I have seen quite a number of pieces of code that do something like the following:

class MyClass {
public:
void setx(int a);
void sety(int b);
int read_x;
int read_y;

private:
int x;
int y;

};

void MyClass::setx(int a) {
x=a;
}

void MyClass::sety(int b) {
y = b;
}

int MyClass::read_x() {
return x;
{

int MyClass::read_y() {
return y;
}

So rather than accessing variables directly(MyClass.x) they use functions to read and set the variable values etc.

Is this a standard or good practice?

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

    Yes, accessor functions are preffered to direct member access for several reasons. They provide a unique access point, are easier to track and to debug.

    For example, instead of setting a breakpoint everywhere in the code where MyClass.x is changed, you can just set a single breakpoint in MyClass::setX().

    However, although better than direct member access, accessor methods are not without their drawbacks, if misused. For details, you can visit Getters and Setters are bad OO design?

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

Sidebar

Related Questions

As the title says, I want to have a build tool that quite much
The title almost says it all. I have a jquery code i've written, im
Exactly what the title says...........any thoughts on other good options for relational database implementation
As title says, how do I call a java class method from a jsp,
The title says it mostly. If I have an object in Python and want
The title says enough I think. I have a full quality BufferedImage and I
as the title says: Anyone know about any good/simple multiple file upload script that
As the title says I use the Rails gem that works good but I
The title says it: I have an excel Sheet with an column full of
As the title says, what I am trying to do is to have jQuery

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.