• 0

[C++] Accessing private member variables


Question

Hi, I'm new to C++ and OOP. I've been having a problem with a concept.

class samp
{
	int i;
public:
	samp(int n)
	{
		i = n;
	}
	int func(samp ob)
	{
		return ob.i;
	}
};

Now, i is a private member of the class samp. So, how can the parameter ob access i directly? I know func(ob) is a member function of the class. But I thought private members can only be accessed with a this pointer.

Can anyone explain why the above code works?

10 answers to this question

Recommended Posts

  • 0
  On 14/09/2011 at 16:08, kInG aLeXo said:

You need the this pointer only if the parameter name is same as a variable name in the class (public or private, because it does shadow the class variable in that case).Otherwise, this is not needed, and is implicitly "included".

I understand that. But, my real question was, how can "ob", which is a parameter of the fuctions func(), directly access ob.i? Doesn't that break the whole idea of having private variables?

  • 0
  On 14/09/2011 at 16:13, Solid Knight said:

Because i isn't private.

This is C++ not Java, i is private by default (he didn't include it in the public, so its by default implicitly private).

  On 14/09/2011 at 16:13, Gigi Buffon said:

I understand that. But, my real question was, how can "ob", which is a parameter of the fuctions func(), directly access ob.i? Doesn't that break the whole idea of having private variables?

I had this very exact mistake in an exam..

Turns out, if the calling class is of the same type, it can actually access the private variables, and even the private constructors and functions of another instance of the same class.(ie: this code works only because func is member of samp class, which is same class of ob parameter, if func was member of another class it won't work).

  • 0

If you know what friend is, you can think of a class being a friend of itself. This allows it to access private members of another object of the same class.

I'm sure there are several reasons for this. The main one I find is that when writing copy constructors you need to access private variables that you might not otherwise want to be exposed.

  • 0
  On 14/09/2011 at 16:16, kInG aLeXo said:

Turns out, if the calling class is of the same type, it can actually access the private variables, and even the private constructors and functions of another instance of the same class.(ie: this code works only because func is member of samp class, which is same class of ob parameter, if func was member of another class it won't work).

Thanks for explaining. I was thinking the same, but couldn't find any reference for it.

  • 0
  On 14/09/2011 at 16:23, Lant said:

If you know what friend is, you can think of a class being a friend of itself. This allows it to access private members of another object of the same class.

I'm sure there are several reasons for this. The main one I find is that when writing copy constructors you need to access private variables that you might not otherwise want to be exposed.

I only know about friend functions, not friend classes. So I can't understand how a class can be a friend of itself. Maybe you meant the member functions of a class are friends of that class? If that's the case, then everything makes sense.

Edit: Just read up on friend classes. Yes, your post makes plenty of sense now. Thank you.

  • 0
  On 14/09/2011 at 16:26, Gigi Buffon said:

Thanks for explaining. I was thinking the same, but couldn't find any reference for it.

In many cases just trust the compiler, even if you find in some reference a different thing.If you can, use GCC instead of Visual Studio (for Windows I recommend Code::Block with MingW), its much better compliant to the C++ standards.

  • 0

" the private keyword specifies that those members are accessible only from member functions and friends of the class. This applies to all members declared up to the next access specifier or the end of the class." private (C++)

It turns out encapsulation is done at the class level, not at the object level. An object of type Foo can access the private members of another object of type Foo and that doesn't break encapsulation because both objects are of the same type. In other words, they know how the other is laid out since it's of the same type. So they're only using information already available to them. Encapsulation is broken when a type Y makes an assumption about type X's implementation.

  • 0
  On 14/09/2011 at 16:34, kInG aLeXo said:

If you can, use GCC instead of Visual Studio (for Windows I recommend Code::Block with MingW), its much better compliant to the C++ standards.

What does this have to do with the OP?

This is just an opinion which imo is plain rubbish. Compliant to what standard? C++11? There are no compilers fully compliant to that.

Imo Visual C++ has a superior debugger than GCC and Visual studio is a better IDE than Code::Blocks.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I wish one of the windows updates hadn't broken glass. Is there a workaround for that I'm not aware of?
    • OpenAI announces o3 Pro, its most intelligent reasoning model by Pradeep Viswanathan OpenAI today announced o3-pro, its flagship reasoning model that uses more compute to "think harder" and provide consistently better answers. This new model will be replacing o1-pro in ChatGPT since it consistently performs better in math, science, and coding. To help everyone make the most out of the model, o3-pro supports tool calling. So, based on the user prompt, the model can do a web search, analyze files, reason about visual inputs, use Python, personalize responses using memory, and more. This is a big improvement over o1-pro, which lacked access to tools. But the downside is that o3-pro will take more time to respond. OpenAI recommends users use o3-pro in cases where reliability matters more than speed. According to OpenAI's evaluations, users consistently preferred o3-pro's responses over o3 in key domains like science, education, programming, business, and writing help. o3-pro was also rated consistently higher for clarity, comprehensiveness, instruction-following, and accuracy. The new o3-pro model is available in ChatGPT's model picker for Pro and Team. ChatGPT's Enterprise and Edu users will get access to this latest model next week. For developers, the o3-pro model comes with a 200,000 context window and is priced at $20 per million input tokens and $80 per million output tokens. OpenAI recommends developers use background mode with o3-pro to prevent timeouts. And the model has a May 31, 2024 knowledge cutoff. The OpenAI team also noted the following as the current limitations of the o3-pro model: At the moment, temporary chats are disabled for o3-pro as we resolve a technical issue. Image generation is not supported within o3-pro—please use GPT-4o, OpenAI o3, or OpenAI o4-mini to generate images. Canvas is also currently not supported within o3-pro. As OpenAI continues to refine its models, the balance between speed and reliability will likely remain a key consideration for users choosing the right tool for their needs.
    • What annoys me is updates take forever to install on my Pixel 9 Pro XL, it was never so slow on my Samsung devices.
  • Recent Achievements

    • Reacting Well
      rshit earned a badge
      Reacting Well
    • Reacting Well
      Alan- earned a badge
      Reacting Well
    • Week One Done
      IAMFLUXX earned a badge
      Week One Done
    • One Month Later
      Æhund earned a badge
      One Month Later
    • One Month Later
      CoolRaoul earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      542
    2. 2
      ATLien_0
      269
    3. 3
      +FloatingFatMan
      209
    4. 4
      +Edouard
      203
    5. 5
      snowy owl
      140
  • Tell a friend

    Love Neowin? Tell a friend!