Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide private members of a Class?

I've been using visual studio for some time and it annoys me everytime when I work with Classes. The problem is, when I create an object of a Class I tend to see the private members belongs to that class and I don't want to, because what if I create a class with 10+ private variable, then it will be a nightmare, there must be a way to hide private members, If there is a way could you please share it with me? Thank you :)

EDIT:

Here is a picture that will help you understand what I'm talking about,

for example here I have 2 private variables of LinkedList class (curSize and head) I won't be able to alter them from main so, there is no point seeing them(is there?) How can I hide them without altering my code? is there a setting for that in Visual Studio?

like image 469
Malkavian Avatar asked Jan 27 '12 00:01

Malkavian


People also ask

Can we access private data members of a class?

Private: The class members declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of the class.

How do you hide a class in CPP?

A preferable solution would be to use a clearly-named namespace such as impl:: or detail:: , which will convey to users that they shouldn't use any classes inside, and stop any possible undesired effects on overloading or the like.

Is class member private by default?

Class members are private by default. Structure members are public by default. Classes can have data as protected members. Structures can have functions as members.


2 Answers

This might not be the best answer nor is it a pretty answer but it get's the job done and if you can live with a small syntax change then it will definitely work. One trick that I learned from observing std classes such as std::vector is that they denote private members with the prefix _, thus forcing all the private members to the very bottom of intellisense. It doesn't remove them from the list but it will move them all to the very bottom so they don't bother you when you are scrolling the list. Here's an example:

class SomeClass{
public:
   int myPublicMemeber;
private:
   int _myPrivateMember;
};
like image 53
idunnololz Avatar answered Sep 28 '22 08:09

idunnololz


Unfortunately, this is not possible in the current version of Visual Studio. In C++, the IntelliSense list is not filtered by accessibility or scope. Therefore, private members are still shown even where they are not actually accessible by your code. There are no settings to tweak this behavior, either.

You just have to rely on the lock icon to indicate that they're private and therefore inaccessible. All of those little icons in the IntelliSense window do have a meaning, you know.

But it looks like this feature might be coming in the next version of Visual Studio (VS11). MSDN says:

List Members Enhancements. The List Members drop-down appears automatically as you type code into the code editor. Results are filtered, so that only relevant members are displayed as you type. You can control the type of filtering logic used by the Member List in the Options dialog box under Text Editor, C/C++, Advanced.

As silly as it is, I'm rather excited about this, too. Along with other cool stuff like better syntax highlighting and reference highlighting. The Developer Preview is out already, so you could try to start using it if you want, but it may not be ready for prime time. And this is admittedly kind of a lousy reason to upgrade...

Alternatively, you could invest in Visual Assist X, which is an extension available for multiple versions of Visual Studio that adds a lot of convenience features to the C++ IDE and, pertinently, improves the IntelliSense filtering. It's not free, but it's pretty awesome for C++ developers, and if I wasn't poor/broke/cheap, I'd definitely buy it myself.

like image 28
Cody Gray Avatar answered Sep 28 '22 06:09

Cody Gray