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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:22:42+00:00 2026-05-22T18:22:42+00:00

Say I have a class with a private data member n and a public

  • 0

Say I have a class with a private data member n and a public get_n() function.
When overloading the output operator for example, I can either use get_n() or make it a friend and use n.
Is there a ‘best’ choice? And if so, why?
Or is the difference going to be optimized away?
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-22T18:22:43+00:00Added an answer on May 22, 2026 at 6:22 pm

    You’ve already gotten a lot of somewhat-conflicting answers, so what you undoubtedly need is one more that contradicts nearly all of them.

    From an efficiency viewpoint, it’s unlikely to make any difference. A function that just returns a value will undoubtedly be generated inline unless you specifically prohibit that from happening by turning off all optimization.

    That leaves only a question of what’s preferable from a design viewpoint. At least IMO, it’s usually preferable to not have a get_n in the first place. Once you remove that design problem, the question you asked just disappears: since there is no get_n to start with, you can’t write other code to depend upon it.

    That does still leave a small question of how you should do things though. This (of course) leads to more questions. In particular, what sort of thing does n represent? I realize you’re probably giving a hypothetical example, but a good design (in this case) depends on knowing a little more about what n is and how it’s used, as well as the type of which n is a member, and how it is intended to be used as well.

    If n is a member of a leaf class, from which you expect no derivation, then you should probably use a friend function that writes n out directly:

    class whatever { 
        int n;
    
        friend std::ostream &operator<<(std::ostream &os, whatever const &w) { 
            return os << w.n;
        }
    };
    

    Simple, straightforward, and effective.

    If, however, n is a member of something you expect to use (or be used) as a base class, then you usually want to use a “virtual virtual” function:

    class whatever { 
        int n;
    
        virtual std::ostream &write(std::ostream &os) { 
            return os << n;
        }    
    
        friend std::ostream &operator<<(std::ostream &os, whatever const &w) { 
            return w.write(os);
        }
    };
    

    Note, however, that this assumes you’re interested in writing out an entire object, and it just happens that at least in the current implementation, that means writing out the value of n.

    As to why you should do things this way, there are a few simple principles I think should be followed:

    1. Either make something really private, or make it public. A private member with public get_n (and, as often as not, public set_n as well) may be required for JavaBeans (for one example) but is still a really bad idea, and shows a gross misunderstanding of object orientation or encapsulation, not to mention producing downright ugly code.
    2. Tell, don’t ask. A public get_n frequently means you end up with client code that does a read/modify/write cycle, with the object acting as dumb data container. It’s generally preferable to convert that to a single operation in which the client code describes the desired result, and the object itself does the read/modify/write to achieve that result.
    3. Minimize the interface. You should strive for each object to have the smallest interface possible without causing unduly pain for users. Eliminating a public function like get_n is nearly always a good thing in itself, independent of its being good for encapsulation.

    Since others have commented about friend functions, I’ll add my two cents worth on that subject as well. It’s fairly frequent to hear comments to the effect that “friend should be avoided because it breaks encapsulation.”

    I must vehemently disagree, and further believe that anybody who thinks that still has some work to do in learning to think like a programmer. A programmer must think in terms of abstractions, and then implement those abstractions as reasonably as possible in the real world.

    If an object supports input and/or output, then the input and output are parts of that object’s interface, and whatever implements that interface is part of the object. The only other possibility is that the type of object does not support input and/or output.

    The point here is pretty simple: at least to support the normal conventions, C++ inserters and extractors must be written as free (non-member) functions. Despite this, insertion and extraction are just as much a part of the class’ interface as any other operations, and (therefore) the inserter/extractor are just as much a part of the class (as an abstraction) as anything else is.

    I’d note for the record that this is part of why I prefer to implement the friend functions involved inside the class, as I’ve shown them above. From a logical viewpoint, they’re part of the class, so making them look like part of the class is a good thing.

    I’ll repeat one last time for emphasis: Giving them access to class internals can’t possibly break encapsulation, because in reality they’re parts of the class — and C++’s strange requirement that they be implemented as free functions does not change that fact by one, single, solitary iota.

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

Sidebar

Related Questions

Let's say I have the following class: class MyClass { private: int Data; public:
So say I have this class: public class PositionList { private Position[] data =
Let's say I have a class called AppConfig: public static class AppConfig { private
lets say I have my owndefined datatype like this: public class InformationType { private
Let's say I have: public class Components<T> extends TupleList<Class<T>, String> { private static final
Let's say I have some Java code: public class SomeClass { static { private
Say, I have following entities: @Entity public class A { @Id @GeneratedValue private Long
Let's say I have following POJO's using Hibernate. public class User { private String
Let's say I have a recursive data structure class Tree { private Tree right;
Say I have a class, User: public class User { private String name; private

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.