• 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

    • Going to quote myself from a comment I made to a related article: It's not about the OS, it's about the workflow. OS fans consistently miss this. People have work to do and they've invested a lot of time, effort and even money building their workflows. It's expensive to change, so that change has to offer real benefits that compensate for the cost of updating workflow and sorry, Win 11 just doesn't. Win 11 breaks workflow in weird and pointless ways seemingly either to dumb down the OS (with the same results as we saw when MSFT tried to dumb down Office's menuing system with "SmartMenus") or make it look more like macOS. MSFT seems to have completely lost the basics of UI/UX like FITS and muscle memory in Win 11. If they had made these optional - then sure, but they aren't. That's the same reason most users won't just jump to an entirely new OS - which has an even bigger workflow cost - until there's just no other option. Not only is there the core workflow cost, but the cost of finding new parallel software for the new OS, transferring and possible converting files and dealing with incompatibilities and then redeveloping workflows. It's just not as simple as "switch". And now there IS another option, stay on Win 10 for another year and pray for Win 12 (much as Win 7 users did with Win 8 - which happened when Win 10 came out).
    • At least that album was really good. I don't know if F1 was $200 million well spent yet...
    • PC manufacturers used to trick BIOS copyright strings to get full editions of trial software by Usama Jawad You may have noticed that when you purchase a new PC, it comes with certain software pre-installed. Sometimes, when you open this software, it activates, and you receive the full version of it without paying any additional cost. This is because that PC's manufacturer is a licensee of that software and the fact that a customer gets the full version of a trial software for free serves as a perk for potential buyers. However, many PC manufacturers tried to trick this process in its infancy. During the days of Windows 95, when the Plug and Play specification was still in development, the OS' engineering team was trying to figure out ways through which it could identify PCs that existed prior to the inception of this specification. To that end, one of the methods they tried was searching for copyright strings and firmware dates in the BIOS. Through the course of this investigation, they discovered a rather oddly named copyright string "Not Copyright Fabrikam Computer" in a PC that was actually manufactured by Contoso. In this case, both Fabrikam and Contoso are fictional names that are used to describe this scenario without revealing the actual identity of the OEMs involved. Microsoft engineer Raymond Chen explains in a blog post that these odd copyright strings were actually appearing because Contoso PCs contained a trial version of a software and the company wanted the full version to be activated for customers even though it was not an official licensee. In order to bypass the costly licensing process, what the firm did was that it added the following text to its copyright string: "Copyright Contoso Not Copyright Fabrikam Computer". The trial version of said software would search for the string "Copyright Fabrikam Computer" and end up finding it within the substring of the convoluted copyright string mentioned above, accidentally activating the software's full version. While more robust ways were adopted later to avoid this problem, it's certainly interesting to see that OEMs would go to this length in order to distribute software that they are not officially allowed to. Well, as they say, the past stays in the past.
  • Recent Achievements

    • First Post
      Myriachan earned a badge
      First Post
    • Week One Done
      DrRonSr earned a badge
      Week One Done
    • Week One Done
      Sharon dixon earned a badge
      Week One Done
    • Dedicated
      Parallax Abstraction earned a badge
      Dedicated
    • First Post
      956400 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      615
    2. 2
      ATLien_0
      227
    3. 3
      +FloatingFatMan
      170
    4. 4
      Michael Scrip
      166
    5. 5
      Som
      148
  • Tell a friend

    Love Neowin? Tell a friend!