When working with native c++ in Visual Studio, intellisense shows private members and functions even when outside the scope of the containing class. This makes it hard to create clean APIs for the classes I write.
Is there a reason for this? Can this be prevented?
Well, why shouldn't it show the private ones as well? They are members, after all, they exist and they are perfectly accessible from certain contexts, just like any other members.
It would be very difficult for the IntelliSense to determine whether the members are accessible or not from this specific context, especially if you take into account that in most cases this context is not yet complete (the user is still typing it up), meaning that it is generally impossible to analyze it.
The reason probably only Microsoft knows. (I think Intellisense doesn't check where you are at the moment, so it doesn't know if you are inside the class (and can access the private members) or outside)
I actually don't know if or how it can be prevented.
But as far as I know, they have an Icon with a lock so you know that they are private. Perhaps that helps
Unfortunetly, this only works for anything YOU make, but its still something to keep in mind if you use a lot of your own libraries.
One thing I did for any libraries I make is to try to trick intellisense with a #define. In my class declaration in the header file for whatever library I'm making, I surround the whole private part in a #ifdef space, for example
#ifdef MYCLASS_SHOW_PRIVATE_VARIABLES
private:
int hideThisVariable;
float noShow;
void HiddenIncrementFunction();
#endif
Then, in the code section of the class where I need to provide definitions for all the methods, at the top before including the file with the class declarations, I add
#define MYCLASS_SHOW_PRIVATE_VARIABLES
This way, the private members are only visible to the methods you implement for your class in the source file. Any clients using this library would not be able to see the private variables via intellisense, unless of course they happen to define your pre processor directive.
精彩评论