7

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?

aligray
  • 2,812
  • 4
  • 26
  • 35
  • @Keoki I'll try a bit harder :) – aligray Jun 18 '11 at 00:19
  • Are you trying to prevent this from happening when clients use your code? I doubt that would be possible. If it is a setting in MSVC, every one who uses your API would need to have it turned on. – RageD Jun 18 '11 at 00:19
  • 1
    @Keoki : There is with some 3rd party Intellisense solutions like Visual Assist X, but there isn't built into Visual Studio. – ildjarn Jun 18 '11 at 00:29

3 Answers3

3

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

Zeus
  • 123
  • 1
  • 6
  • 1
    Of course it could be prevented. Intellisense in C++ is nowhere near as good as it is for .NET projects. Given, I'm sure it is more difficult to implement for C++. – Ed S. Jun 18 '11 at 00:35
  • 1
    @Ed S.: Have you used it in VC 2010? It's pretty much on par with .NET at this point. – Billy ONeal Jun 18 '11 at 01:05
  • @Billy: Actually that's a good point and it's funny I left that out. I used VC10 just last night for the first time and the background compilation and intellisense were both very nice. This was a C project, not C++, but still, very good. – Ed S. Jun 18 '11 at 01:29
3

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • I think I'll just learn to live with it. – aligray Jun 18 '11 at 01:04
  • 4
    Intellisense is so productive because it narrows down to the most specific scope possible. If every time you started typing it showed every single class in every single namespace in every assembly it wouldn't be as effective. Instead it's job is to narrow the scope as much as possible. It will only show types in the currently loaded assemblies and in the currently imported namespaces. When selecting members it should only show members that are in scope, those publicly accessible. – Despertar May 12 '12 at 02:31
  • 1
    -1 Visual Studio uses the EDG compiler front end for its IntelliSense support. There's nothing difficult about determining the access level of any given symbol in any given context. EDG provides a full AST. – IInspectable Nov 14 '13 at 19:02
  • @IInspectable: Misguided. It is not enough to analyze the class (even when the class definition is complete). It is also necessary to analyze the current context and determine its access rights. And, as I said above, it is generally impossible, regardless of the front end used, since the current context is typically is not even syntactically complete. – AnT stands with Russia Nov 05 '14 at 21:46
0

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.

Nkosi Dean
  • 227
  • 8
  • 18
  • 2
    Dude wouldn't that change the size of your classes based on whether that define is there or not? If anyone stack allocates your classes you'll be screwed. – deemen Mar 18 '13 at 22:11
  • Worst. Idea. Ever. Get a proper tool that can be adjusted to provide information the way you feel comfortable. Don't adjust your code to compensate for poor tools. And don't ever change your code in a way that completely breaks it. – IInspectable Nov 14 '13 at 19:07