问题
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?
回答1:
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.
回答2:
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;
};
回答3:
You can use regions, like this:
class MyClass {
#region Private Variables
private int x;
private int y;
private int z;
#endregion
}
Visual Studio will display a little -
next to the #region
line. Click it to hide the variables.
来源:https://stackoverflow.com/questions/9027338/how-to-hide-private-members-of-a-class