Archived

This topic is now archived and is closed to further replies.

HellRiZZer

Hiding data members... How?

Recommended Posts

Hi guys, I''ve been wondering how do you hide data members of a class? For example, if I use MSVC and press -> on pointer of object (e.g obj->), it gives me a list of all functions and data members. I don''t want a user to know about data members, that''s what Get/Set functions are for. So, how do I do that? Thanks. " Do we need us? "

Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don''t know much about intellisense but I know about C++.
If I''m right all protected and private members of a class should be automatically hidden.

class foo
{
public:
Get();
Set();
private:
int a;
}

Share this post


Link to post
Share on other sites
Nah, I know about private/protected and its not that.
Like if you are programming in MFC, you can type "this->" and it will give you heaps of member CWnd functions (or CAfxView or whatever), but almost no data members (except hwnd and some other).

Thanks.

" Do we need us? "


Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
dont worry, all private and protected members appear in the list but you wont have access to them. just try it.

class foo{
private:

int foobar;

public:
...
}

int main(){

foo *footest;

footest->foobar = 3; //major error!!

return 0;
}

Share this post


Link to post
Share on other sites
If you are really paranoid you could declare a private struct to hold all of your data members, the user would see no data members apart from the struct, but because the declaration was private they could not see any of the structs members.


EDIT: you could also provide a virtual interface class in your header with a static member function that will return a specific implementation of that interface. You then write out the derived class that implements the interface in the .cpp file, That way the other users won't see anything other than the member funcitons that you allow them.

[edited by - gleno on October 9, 2003 7:11:15 AM]

[edited by - gleno on October 9, 2003 7:11:48 AM]

Share this post


Link to post
Share on other sites
The reason why you don''t see any other data members in CWnd, for example is: There are no other data members. If you really want to hide the data use an interface class and don''t put the actual implementation into a public header file.

Share this post


Link to post
Share on other sites
Create an interface as follows:



#include <windows.h>

DECLARE_INTERFACE(IMyClass) {
virtual void Function1() = 0;
virtual bool Function2() = 0;
virtual char * Function3() = 0;
};

//...


class MyClass : public IMyClass {
private:
int var1;
bool var2;
// ...

public:
MyClass();
virtual ~MyClass();

virtual void Function1();
virtual bool Function2();
virtual char * Function3();
};




This will only show the objects/methods in the interface and not the class (including private members), because you onyl see the interface, and not the class (with definitions).

[edit] Just to clarify, you will be instantiating the interface through a method that creates the "MyClass", but returns an "IMyClass" object (instance).

www.cppnow.com

[edited by - superdeveloper on October 9, 2003 9:55:30 AM]

Share this post


Link to post
Share on other sites