Whole Tomato Software Support Forum
Whole Tomato Software Support Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Other
 The Lounge
 C++ properties
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

rblondeau
Tomato Guru

Canada
102 Posts

Posted - Dec 09 2004 :  7:59:37 PM  Show Profile  Visit rblondeau's Homepage
feline,

Referring back to http://www.wholetomato.com/forum/topic.asp?TOPIC_ID=3299

You mention that strName.Empty looks kind of funny when thinking in terms of C++ (the previous topic was on C#).

In C++, strName.Empty would actually look correct if Empty was a member variable and didn't have any hungarian notation.

Also, it could be a property that was defined using the __declspec modifier (this is Microsoft specific).

If you've never come across __declspec, it's something you should look into. It lets you define a single property, that depending on whether it is being read from or written to, will call accessor functions to accomplish the same goal.

For example:

__declspec(property(get=GetMessageNo, put=SetMessageNo)) long MessageNo;

This statement indicates that a property called MessageNo exists, and that it has a datatype of long.

When the property is being read from it will use the GetMessageNo() function to return a long value.

If the property is being written to, it will call the SetMessageNo(long lMessageNo) function and pass the long value into it as a parameter.

Doing this lets you create functions that do a whole bunch of processing (if you want) prior to storing or returning the desired long value.

You can even choose to omit either the get or set portions of the statment. That lets you create read-only, and write-only properties.

The only catch to this modifier (other than it being Microsoft specific) is that the property must be written in the public section of a class. you cannot make the property protected or private for the internal use of the class only.

As a side note, it is pretty confusing to use a property from within the class that defined it. I think it is much clearer to simply use the appropriate accessor method(s). Only use property from an external routine.

feline
Moderator

United Kingdom
12804 Posts

Posted - Dec 10 2004 :  3:14:17 PM  Show Profile
*studies this* *thinks about this*
this looks really odd to me. i still do a lot of my work in C on UNIX, using VIM. as a result of the high tech IDE we have available to us i am a committed user of my own form of Hungarian notation. even in the .NET IDE with all the power of VAX i still find it to be invaluable. i can just tell what variables are without having to dig the mouse out to investigate them

thinking back, the comment about "Empty" looking so odd in the C# code comes from three things:
a) the original post way saying that it was being inserted as a function call, ie with brackets on the end.

b) there is no sign of Empty in the ctrl+space list on the class, which i would normally take to mean that Empty is invalid at this point in the code.

c) i see "Empty" and i instantly think of the "make this thing empty" function call, which is an assumption on my part *blush*


if i have understood __declspec correctly you need to define and create the two access functions yourself, before you can use them like this. at this point, i am inclined to ask, why do this?

the other question is, is this that C# properties thing i keep on seeing references to? i still haven't really worked out what it is

as another programmer reading your code, how would i know that you were using Empty as a C++ property? doesn't this lead to confusion, since the code does not do what I think it is going to do?

at least when you call the standard accessor pair functions what you are doing is clear.

i am coming to share the view point that the most important thing when writing code is to make sure it is easy to maintain 2 years later, when i have to come along and perform a major re-write *sigh* my instinct is to say that using C++ properties makes the code harder to maintain, since it is less obvious.

do you use them yourself? how do you find they work for you in reality?

with regard to the code being Microsoft specific, we use Qt at work, a cross platform development API to do all of our UI code, and people talk about recompiling our product for the MAC or under Linux now and then. Microsoft specific extensions are probably a bad habit to get into under these conditions.

zen is the art of being at one with the two'ness
Go to Top of Page

rblondeau
Tomato Guru

Canada
102 Posts

Posted - Dec 10 2004 :  6:45:24 PM  Show Profile  Visit rblondeau's Homepage
I get my familiarity with setting/getting values to/from an object via it's properties from doing some programming in Visual Basic.

Properties make it very easy to Get or Set a value from an object.

Aren't the following first two lines easier to read, and much more descriptive than the next two?

// With properties
if(oObj.Full) { oObj.Empty(); }
oObj.Full = false;

// Without properties
if(oObj.Full()) { oObj.Empty(); }
oObj.Full(false);

------------------------------------

I do use properties in my objects, and like I said in my earlier post, I have found that it is only beneficial to use an object's properties from outside of the object itself (ie: Another routine or object accesses the object's properties.) If I use them within the class itself, I get lines that are fairly confusing like the following:

class oObj
{
   void SomeRoutine(void)
   {
      ...
      Full = false; // This makes me think "What's a 'Full' "
   }
}

-------------------------

A definite benefit of properties is to fall into line with how all the newer programming languages seem to be going. That makes it easier to switch between the languages, if they all have some of the same features.

Don't forget that there's nothing that stops you from using the accessor functions.
Also, you don't always need to specify both accessor functions in the property. You could simply specify the get function which would effectively make the property read-only.

quote:
how would i know that you were using Empty as a C++ property?


My answer to that is another question: Have you ever written a class or struct who's members do not have hungarian notation?

For Example:

struct Car
{
   int Doors,
   bool Sunroof
} MyCar;

In this case I could indicate that MyCar has 2 doors and no sunroof simply by doing the following:

MyCar.Doors = 2;
MyCar.Sunroof = false;

How do I know, just by looking at this code, that these are variables? They look like properties to me (because that's what I'm used to).

In reality these members are behaving exactly like properties except that there is no processing that can occur. You simply set or get the values.

quote:
b) there is no sign of Empty in the ctrl+space list on the class, which i would normally take to mean that Empty is invalid at this point in the code.


I don't know if the project has to be built first, but I just tried what you say and the Property I made does show up in the listbox as a Public Variable.

[quote]the other question is, is this that C# properties thing i keep on seeing references to?[quote]

Yes basically you are correct except that Properties in C# are implemented in a cleaner way than they are in C++.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Whole Tomato Software Support Forum © 2013 Whole Tomato Software, Inc Go To Top Of Page
Snitz Forums 2000