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
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
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.
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.
Oups.... found a problem.
I nedd to hide get_size() and set_size() from the user. Can this be done
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.
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.
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...
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.
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) .
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:
becomes:
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.
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
}
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.