A question about programming style
-
Hi, In Visual Basic, when working with objects, we have the following syntax:
obj.property=value or variable=obj.property call obj.method([params])
Remember that theproperty
is not only member variable which is declared with public modifier but theproperty
can (and in fact its always) be a function. So we do not need to know where to storevalue
or where the data ofvariable
come from inside the object. More over, function inside property allows us to do few calculations based on value set or got. My question here is: In Visual C, Could I implement such programming style, by any way? At present, I implement such style by declaration a member variable as public member, but it may not good when working with object. ImplementationSet
,Get
functions do not allow VB programming style. Thanks for reading. -
Hi, In Visual Basic, when working with objects, we have the following syntax:
obj.property=value or variable=obj.property call obj.method([params])
Remember that theproperty
is not only member variable which is declared with public modifier but theproperty
can (and in fact its always) be a function. So we do not need to know where to storevalue
or where the data ofvariable
come from inside the object. More over, function inside property allows us to do few calculations based on value set or got. My question here is: In Visual C, Could I implement such programming style, by any way? At present, I implement such style by declaration a member variable as public member, but it may not good when working with object. ImplementationSet
,Get
functions do not allow VB programming style. Thanks for reading.Extracted from MSDN:
**__declspec( property( get**=get_func_name **) )** declarator **__declspec( property( put**=put_func_name **) )** declarator **__declspec( property( get**=get_func_name, put=put_func_name **) )** declarator
This attribute can be applied to non-static “virtual data members” in a class or structure definition. The compiler treats these “virtual data members” as data members by changing their references into function calls. When the compiler sees a data member declared with this attribute on the right of a member-selection operator (“.” or “->“), it converts the operation to a get or put function, depending on whether such an expression is an l-value or an r-value. In more complicated contexts, such as “+=“, a rewrite is performed by doing both get and put. This attribute can also be used in the declaration of an empty array in a class or structure definition. For example:__declspec(property(get=GetX, put=PutX)) int x[];
The above statement indicates thatx[]
can be used with one or more array indices. In this case,i=p->x[a][b]
will be turned intoi=p->GetX(a, b)
, andp->x[a][b] = i
will be turned intop->PutX(a, b, i);
Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
-
Hi, In Visual Basic, when working with objects, we have the following syntax:
obj.property=value or variable=obj.property call obj.method([params])
Remember that theproperty
is not only member variable which is declared with public modifier but theproperty
can (and in fact its always) be a function. So we do not need to know where to storevalue
or where the data ofvariable
come from inside the object. More over, function inside property allows us to do few calculations based on value set or got. My question here is: In Visual C, Could I implement such programming style, by any way? At present, I implement such style by declaration a member variable as public member, but it may not good when working with object. ImplementationSet
,Get
functions do not allow VB programming style. Thanks for reading.be careful, OOP is NOT available in C (so either in Visual C). object programming comes with C++. And for your question, yes, you can. Classes are made for such. You put your data members eito private or protected statements (in general) as properties, and the interface for your classe, defined with your function members ("methods" in VB) are in a public statement. All of this is not definitive, but it is a cool way of programming properly.
TOXCCT >>> GEII power
-
Extracted from MSDN:
**__declspec( property( get**=get_func_name **) )** declarator **__declspec( property( put**=put_func_name **) )** declarator **__declspec( property( get**=get_func_name, put=put_func_name **) )** declarator
This attribute can be applied to non-static “virtual data members” in a class or structure definition. The compiler treats these “virtual data members” as data members by changing their references into function calls. When the compiler sees a data member declared with this attribute on the right of a member-selection operator (“.” or “->“), it converts the operation to a get or put function, depending on whether such an expression is an l-value or an r-value. In more complicated contexts, such as “+=“, a rewrite is performed by doing both get and put. This attribute can also be used in the declaration of an empty array in a class or structure definition. For example:__declspec(property(get=GetX, put=PutX)) int x[];
The above statement indicates thatx[]
can be used with one or more array indices. In this case,i=p->x[a][b]
will be turned intoi=p->GetX(a, b)
, andp->x[a][b] = i
will be turned intop->PutX(a, b, i);
Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control