473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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?

Aug 2 '07 #1
2 1295
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
Aug 2 '07 #2
You get the answer here:

http://www.parashift.com/c++-faq-lit...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() = 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ão Helou Neto.

Aug 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: WindAndWaves | last post by:
Dear All Can you tell me why you use a class module??? Thank you Nicolaas ---
1
by: Nathan Kovac | last post by:
I am not sure why this wasn't sent last week. I keep posting a reply but it just doesn't go. Here it is again. I have inserted answers below. By the way the error is back today, so if you...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
14
by: pamelafluente | last post by:
Hi guys, I have a hard problem :) I have the following situation: <span onmouseover = "SelectionEffect()"> <div style="width: 50px; height: 50px; background-color:#FFE4E1"></div> </span>
28
by: VK | last post by:
A while ago I wrote a "Vector data type" script using DOM interface to select.options. That was a (beautiful) mind game :-) rather than a practical thing to use. Here is another attempt open...
3
by: martin | last post by:
just wondering.... Say i have a class with 3 nonstatic properties P1, P2 and P3. Now somebody creates an instance of my class and calls any of them (instance1.P2) Is it possible to execute some...
4
by: Michael Lang | last post by:
I was basically wanting to know how to use the System.Configuration namespace to be able to load an arbitrary number of unknown attributes on an element in a custom section in the .config into a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.