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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:13:22+00:00 2026-05-13T23:13:22+00:00

Suppose I have a class Foo , with a private variable bar_ containing some

  • 0

Suppose I have a class Foo, with a private variable bar_ containing some state for Foo. If necessary, I may write public get/set methods for bar_. Naturally, I avoid this as much as possible to maintain encapsulation.

Assuming I have these get/set methods, whenever I have to access or modify bar_ within a method belonging to Foo, I usually do it directly to bar_, instead of using the get/set methods, which I use for accessing bar_ from outside the class. I have no justification other than concerns regarding the speed of directly accessing the variable versus calling the methods, but I suspect that if the get/set methods are defined inline (which they are) it shouldn’t make a difference. Does it make a difference? Does constness play a role in this?

So far I haven’t had any problems with this, but I have a lingering feeling I am Doing It Wrong. Are there any compelling arguments for not doing it? Any guidelines regarding this?

  • 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-13T23:13:23+00:00Added an answer on May 13, 2026 at 11:13 pm

    I have no justification other than concerns regarding the speed of directly accessing the variable versus calling the methods, but I suspect that if the get/set methods are defined inline (which they are) it shouldn’t make a difference. Does it make a difference? Does constness play a role in this?

    The inline keyword hardly plays a role in whether or not the compiler does any inlining. The use for the keyword in that regard is essentially deprecated. Modern compilers inline like crazy, and they know when to do it better tan any programmer does.

    Any compiler worth dirt will say “Hm, call this function to get this member variable; hey I can just get the member variable!” You’re worrying about nothing. This happens regardless of any inline keywords.

    That said, I almost always use the member functions. If I change how a variable behaves when it’s accessed, I now “automatically” apply that everywhere it’s used. Clean code should be your goal, though, not a dogmatic “always skip functions” or not.

    Anytime I just want a variable value, I use the corresponding member variable. (i.e, if I were writing std::vector, if I needed to check if the size was less than something, I’d say size() < x). But if it’s cleaner to use the variable directly, do that instead, such as mSize++.

    const-ness is irrelevant. If you’re in a non-const function, you’ll use the non-const version of your getter, same with const. Obviously, using the members directly maintains const-ness. There is no difference.

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

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer the public field 'what' ("User-defined message code so that the… May 15, 2026 at 4:55 pm
  • Editorial Team
    Editorial Team added an answer If you've deleted everything, there is no leak, by definition.… May 15, 2026 at 4:55 pm
  • Editorial Team
    Editorial Team added an answer Why not just use taxonomy for the locations, do a… May 15, 2026 at 4:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.