CyberLord_Dan

Hi!

Using .net framework, you can create sth like this:

public bool variab

{

get{return _vari;}

set{_vari=value;}

}

I need to do this within a native project (Win32). How do I do this


Re: Visual C++ Language get , set native

Jonathan Caves - MSFT

You can't - native C++ does not have direct support for properties. There is a rather long winded way to do this - but it is not nearly as pretty.

Code Block

class X

{

public:

size_t __declspec(property(get=get_Size, put=set_Size)) Size;

size_t get_Size() const

{

return size_;

}

void set_Size(size_t size)

{

size_ = size;

}

private:

size_t size_;

};

void f(X* pX)

{

pX->Size = 4;

}

As I said it is not as pretty or as simple - but it does work - we use it within the compiler a lot.

Note: 'put' was choosen instead of 'get' because at the time (circa Visual C++ 5.0) this is what VB programmers used.






Re: Visual C++ Language get , set native

CyberLord_Dan

tnx. this works just fine





Re: Visual C++ Language get , set native

CyberLord_Dan

Oups.... found a problem.

I nedd to hide get_size() and set_size() from the user. Can this be done





Re: Visual C++ Language get , set native

Jonathan Caves - MSFT

What exactly do you mean Can you give an example

Properties are just syntactic sugar so under the covers the compiler still needs to be able to access the get/set methods.






Re: Visual C++ Language get , set native

CyberLord_Dan

I mean setting set_size as a private or internal. (I don't want IntelliSense to show it). You know how is the .net framework.





Re: Visual C++ Language get , set native

Jonathan Caves - MSFT

In native C++ internal does not exist. You can make the set_ function private in which case the user can see it but they cannot call it or you can leave it out entirely - in which case you have a read-only property.






Re: Visual C++ Language get , set native

CyberLord_Dan

i've tried the code:

class X

{

public:

size_t __declspec(property(get=get_Size, put=set_Size)) Size;

private:

size_t get_Size() const

{

return size_;

}

void set_Size(size_t size)

{

size_ = size;

}

size_t size_;

};

void f(X* pX)

{

pX->Size = 4;

}

but I get an error...




Re: Visual C++ Language get , set native

Ramkrishna Pawar

You can't use it like a property in native C++ code, you need to invoke the public interfaces (functions) to do that, in your case you must have the get and set functions public and then from outside you can invoke them to do stuff.






Re: Visual C++ Language get , set native

einaros

Well that works as you'd expect it to. Both the setter and getter is private, so the f function does not have access to use it.

Are you trying to make the getter and setter accessible, but just hide it from intellisense If that's the case, I don't think that's possible, nor a very good design choice. The class definition is likely to be the reference users of your class turn to for guidance -- not intellisense.





Re: Visual C++ Language get , set native

CyberLord_Dan

The main fact is that i don't want the user to be able to acces set and get functions directly. i want the user to be able to acces just the Size variable (property) .




Re: Visual C++ Language get , set native

einaros

That cannot be done. And the effect really is the same. Whether the user calls set_/get_ or accesses your variable doesn't make much of a difference. Using the functions directly is going to make him vulnerable to interface changes, should Size become a variable all of a sudden, but other than that -- I can't see any problems.





Re: Visual C++ Language get , set native

Jonathan Caves - MSFT

As I said earlier the properties are just syntactic sugar - the first thing the compiler does when it sees a use of a property in some code is to immediately turn around and convert the code to use the appropriate method. For example:

Code Block
pX->Size += 4;

becomes:

Code Block
pX->set_Size(pX->get_Size() + 4);

So the get_/set_ methods need to be accessible - there is no way to avoid this.

C# manages to hide the get_/set_ methods from the user because it has meta-data which is a very rich representation of the code and so the C# Intellisense engine knows that these methods are "special" and it can hide them. Native C++ doesn't have the luxury of meta-data and hence the C++ Intellisense engine has to assume that these are real methods (which they are). After all it is perfectly legal for a C++ user to have a class which has get_Size and set_Size methods which are not associated with a property.






Re: Visual C++ Language get , set native

CyberLord_Dan

K I got it.

One more question:

Is it posible to execute a specific function when a variable changes its value For example:

int x;

..........................

..........................

void setX(int newX)

{

x = newX; /////// when this code occurs, the function showError() to occur.

}

void showError()

{

........... // my error code

}





Re: Visual C++ Language get , set native

Ramkrishna Pawar

Smile well, I want to say 'no', but if you want to sooo do it, you can write a thread which keeps watching that variables value, let me also tell you that this kind of mechanism is a realy ugly way to program.