tags:

views:

160

answers:

6

I've found a few methods online on how to implement property-like functionality in c++. There seems to be some sound work-arounds for getting it to work well.

My question is, with the prevalence of properties in managed langues, should I spend the effort and the possibilty of code-breakage (or whatever) to implement properties in my code?

Say I'm going to dev up a library of calls for someone else to use, would properties be desired enough to validate the extra code?

+9  A: 

What can you gain from doing this that you can't get from just using accessor functions? It seems to me that while coding you ought to play to a language's strengths instead of getting it to emulate another language. So I'd vote no on this one. However, if you are writing in MANAGED C++ and are going to be using this code to interface with C# on a regular basis, and if for some reason you wanted to make the backend more usable by a native C# programmer, it might be worthwhile.

Rokujolady
It was more just syntactic in nature. I'm currently working on a project with a few friends who code primarily in c# while I do mainly c++. The project is in standard c++, but they were griping about a lack of properties.
Veaviticus
One advantage of something like a property over a private member with accessors: you can take a reference to it.
Mike Seymour
@Mike: this isn't relevant if the desire is to have "properties like C#", since you can't have a reference to (or pass by reference, etc) a property in C#.
Pavel Minaev
@Veaviticus: there are numerous points at which C++ deviates from C# in much more significant ways. I find it surprising that the mere syntactic sugar that is C# properties is a point of contention compared to everything else.
Pavel Minaev
A: 

Properties actually prevent code breakage if done properly. It allows you to change the implementation of the property underneath without the caller having to change his code or even worry about it.

For instance, say you have a Socket class that takes a socket number. Implement as a property to just take an int and store it.

However your boss says that you should not accept socket numbers lower than 1024. Your property can change to scan for that and not accept the value.

No change to caller code.

Edit: A slight mis-understanding of the question...I took properties to mean normal accessor functions.

gbrandt
@gbrandt: If your answer isn't pertinent to the question, then you should delete it.
Adam Robinson
+4  A: 

Unless you add reflection to the mix (being able to identify at runtime what properties exist on an object), properties are nothing more than syntactic sugar for getters and setters. Might as well just use getters and setters, in that case.

Properties with reflection can indeed be useful for C++ programs, though. Qt handles this quite nicely.

Cogwheel - Matthew Orlando
+1 but I'd disagree about QT handling it so nicely. Anyone could handle reflection/introspection 'nicely' with a separate compiler/preprocessor that adds extensions to the C++ language.
@stinky472: True, but they could also do a really bad job of it. ;) I just think Qt's implementation is nice in its own right.
Cogwheel - Matthew Orlando
+4  A: 

Properties are not idiomatic Standard C++ - as evidenced by the fact that there's no single "property-like" library in widespread use. The complexity of properly implementing them in conformant C++ is significant, while the benefits compared to direct invocation of accessor methods are small, and mostly stylistic. In my opinion, it's not worth the bother.

Pavel Minaev
QT has pretty wide-spread use with properties. I don't particularly care for it but reflection is useful for designing widgets through property editors.
Pavel Minaev
A: 

I have once tried to implement something similar to Matlab structures in C++, i. e. structures to which one can add named fields. After that I became a firm believer in the "Do Not Fight the Language" principle.

Dima
A: 

Efforts to make one programming language behave like another are often a special case of the inner platform effect.

I agree with others - just use getters and setters.

If you really need properties in C++, perhaps you already have them as a language extension. I think Visual C++ does - almost certainly for managed C++, but maybe for unmanaged too. Personally, the only reason I'd use them is in managed C++ to fit with .NET conventions.

Outside of that, creating an inner platform for this will almost certainly cause more problems than it solves.

Steve314