An attempt to make a 'property' in C++



 DEVELOP > c-Plus-Plus > An attempt to make a 'property' in C++

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Pavel Shved"
Date: 02 Aug 2007 07:00:18 AM
Object: An attempt to make a 'property' in C++
Hello.
Some languages contain so-called 'properties' - class members that
behave in different ways depending on whether you read or modify them.
I wonder whether is can be declared like this:
class A{
...
int& Prop()
{
//When we write to some kind of field
m_modified = true; //Lets assume for example, that we need to
know
//whether we use property for writing.
return m_field;
};
const int& Prop() const
{
//When we read something from field
return m_field;
};
};
Will the non-constant function be invoked only when the value returned
is being modified?
.

User: "Victor Bazarov"

Title: Re: An attempt to make a 'property' in C++ 02 Aug 2007 07:25:17 AM
Pavel Shved wrote:

Some languages contain so-called 'properties' - class members that
behave in different ways depending on whether you read or modify them.
I wonder whether is can be declared like this:

class A{
...

int& Prop()
{
//When we write to some kind of field
m_modified = true; //Lets assume for example, that we need to
know
//whether we use property for writing.
return m_field;
};
const int& Prop() const
{
//When we read something from field
return m_field;
};

};


Will the non-constant function be invoked only when the value returned
is being modified?

No. Non-constant function will be invoked for any non-constant 'A' object.
Why reinvent the wheel? Look on the Web for examples of decent property
implementations.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.

User: "=?iso-8859-1?q?Elias_Salom=E3o_Helou_Neto?="

Title: Re: An attempt to make a 'property' in C++ 02 Aug 2007 08:48:26 AM
You get the answer here:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.12
It mainly states that the const member function will be called only
when the class instance calling it is itself const. The following
program shows it:
#include <iostream>
class ConstTest
{
public:
ConstTest( void ){}
const bool& get( void ) const
{
std::cout << "Const.\n";
return( _b );
}
bool& get( void )
{
std::cout << "Not const.\n";
return( _b );
}
private:
bool _b;
};
int main( int argc, char* argv[], char* env[] )
{
ConstTest obj1;
const ConstTest obj2;
obj1.get();
obj1.get() =3D false;
obj2.get();
}
Compile and run the above program to get:
Not const.
Not const.
Const.
I guess that you'd better stick to the set/get names. Just rename the
non-const get() to set() and you are done. If you are going to write
two separate functions to each property anyway, why not to do it?
If lots of attribute of your class are to be set/get as a property you
may end up writing much less code by providing a template class to the
properties.
That's all, I guess.
Elias Salom=E3o Helou Neto.
.


  Page 1 of 1

1

 


Related Articles
Template "redefinition" linker error. (attempt #3)
Template "redefinition" linker error. (attempt #2)
how to initialize static references to an object, second attempt
2nd Attempt : How can I count the actual number of operations performed in a program
Attempt at initialising a class with a vector "inline"
Re: Yet another Attempt at Disproving the Halting Problem
How Dare Could America Industrial Property Office Be In Conspiracy With Jungang International Patent Office To Make An Extravagant International Crime ?
Designing a property system
Does c_str property of string class allocate memory?
C++ property file
QT : Homogeneous cells size in QGridLayout? (--> Gtk::Table homogeneous property)
How to implement "__property" in ANSI C++ ?
Property pages in ATL COM add-in
BGL Colormap Property
Class and Property Representation in image
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER