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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:19:26+00:00 2026-06-06T22:19:26+00:00

What are the difference between a public class member class data { public: std::list<data>

  • 0

What are the difference between a public class member

class data
{
public:

    std::list<data> list_of_data;
};

And a method that returns a private member as reference?

class data
{
public:

   std::list<data>& get_data()
   { return list_of_data; }

private:

    std::list<data> list_of_data;
};

Which one is better?

  • 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-06-06T22:19:27+00:00Added an answer on June 6, 2026 at 10:19 pm

    The question doesn’t make much sense in that form. If you publicly broadcast the fact that in the second case the list_of_data is indeed a member of data (even though it is private), then there’s really no difference.

    But that’s not what we have private members for. In typical C++ design, in your second variant the outside code is not allowed to use any knowledge about private members of data. The only thing the outside code has knowledge about is the public get_data() member. Where that get_data() goes for the actual data – the outsiders don’t know and don’t care.

    In this case the difference becomes quite noticeable.

    • In the first case you are exposing the fact that list_of_data is physically present as a member of class data. For example, it immediately means that the lifetime of list_of_data is the same as lifetime of the corresponding data instance. It also means that different data instances have different list_of_data members.

    • In the second case you are not exposing anything like that. The outside code does not know where the actual std::list<data> object is located and what’s its lifetime. The outside code does not know whether different instances of data will return different references from their get_data() members. In order to get answers to these questions the outside users have to pay attention to the intended design of the code, instead of jumping to conclusions by reading the code. And it is a good thing.

    This is the whole reason we often use accessor functions (even reference-returning ones) instead of exposing data members publicly.

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

Sidebar

Related Questions

What is the difference between: public class A { private int x = 1;
Is there a functional difference between the following syntax... [Foo, Bar] public class Baz
what would be the difference between this class Class1 { public string prop1 {
In PHP, What is the difference between declaring methods inside class like public function
Is there a difference between the following two : ArrayList list = getData(); public
Is there a runtime performance differance between public and private variables/methods? I know that
Recently I've been thinking about performance difference between class field members and method variables.
Please could someone explain me what's the difference between public and published class members
What the difference between a static member variable of a class and a free
I'm currently working on a class that calculates the difference between two objects. I'm

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.