473,322 Members | 1,307 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,322 software developers and data experts.

const methodes and lazy calculation

Hi,
I would like to know whether a certain coding pattern exist to handle
the following problem.
I am using a const methode to get and value a from the class. In case
the value was not calculated yet, I want to start the calculation
(which is not a const methode) and return the value. But since the
calculation is not const, I can't call it within the getter. Any ideas?

Thanks in advance,
Thomas Kowalski

Jun 8 '06 #1
7 2216
th***@gmx.de wrote, On 8.6.2006 11:07:
Hi,
I would like to know whether a certain coding pattern exist to handle
the following problem.
I am using a const methode to get and value a from the class. In case
the value was not calculated yet, I want to start the calculation
(which is not a const methode) and return the value. But since the
calculation is not const, I can't call it within the getter. Any ideas?

Thanks in advance,
Thomas Kowalski

You can use keyword "mutable" on member variables so that you can change
them in const methods.

--
wilx
Jun 8 '06 #2
> You can use keyword "mutable" on member variables so that you can change
them in const methods.


means
1) make the getter const
2) make the value a mutable
3) create a private const methode to make the calculation

I guess this is already the cleanest way. Thanks. Is there something
like mutable for methodes, too? Or does just a const_cast of this help
there?

Thanks,
Thomas

Jun 8 '06 #3

th***@gmx.de wrote:
Thanks. Is there something
like mutable for methodes, too? Or does just a const_cast of this help
there?

Thanks,
Thomas


There is no such thing as:

class X
{
public:
void getY() mutable;
};

presumably getY() would be a method that can be called from const
functions even though it is not itself a const method (i.e. it may
modify anything).

Doesn't seem to make sense to have it there though as if it could
modify anything, your const methods shouldn't be calling it.

The only time it is useful to const_cast in such a situation is when
you have 2 overloads that use the same method to return an address or
reference to some class member, one of them const and one non-const.
Then you would normally implement one to call the other, and then it is
legitimate to use const_cast because it is YOUR implementation and you
know what you are doing (that you are not actually breaking any
contracts).

There are workarounds to that, eg having some external template method
that does the calculations where the template parameter can be T or
const T. Those who love to stick to coding standards and have a coding
standard that says "never const cast" would probably do it that way.
Practical programmers will often find the const_cast method the
clearest option for the situation.

Jun 8 '06 #4
Hi Thomas,

th***@gmx.de schrieb:
Hi,
I would like to know whether a certain coding pattern exist to handle
the following problem.
I am using a const methode to get and value a from the class. In case
the value was not calculated yet, I want to start the calculation
(which is not a const methode) and return the value. But since the
calculation is not const, I can't call it within the getter. Any ideas?

Thanks in advance,
Thomas Kowalski


One way to implement that lazy calculation in const getter-methods is to
declare the calculation method const and the member that should be
calculated mutable. This allows to change the value of that member in
const methods.

Ciao,
Marco
Jun 8 '06 #5

<th***@gmx.de> wrote in message
news:11*********************@y43g2000cwc.googlegro ups.com...
Hi,
I would like to know whether a certain coding pattern exist to handle
the following problem.
I am using a const methode to get and value a from the class. In case
the value was not calculated yet, I want to start the calculation
(which is not a const methode) and return the value. But since the
calculation is not const, I can't call it within the getter. Any ideas?

Thanks in advance,
Thomas Kowalski


How about not making the get method const? If it changes the value of a
member (even if just once, when first called), then it's not following the
promise to NOT change any members, right? You can return a const result,
but that doesn't mean you have to declare the method itself as const.

-Howard
Jun 8 '06 #6
Hi,
first thanks for all the answers. They have been very helpfull to order
my thoughts :)
How about not making the get method const? If it changes the value of a
member (even if just once, when first called), then it's not following the
promise to NOT change any members, right?


Thats true, actually I thought about that, too. But if you just see the
interface, there is no change visible for the user of the class (class
A). Just the calculation might be a bit faster. Besides I want the
class to be part of another one (class B). And I want B to have a
getter that returns a const reference to class A so that the user can't
do "stupid things".

Thomas

Jun 9 '06 #7
th***@gmx.de wrote:
...
How about not making the get method const? If it changes the value of a
member (even if just once, when first called), then it's not following the
promise to NOT change any members, right?


Thats true, actually I thought about that, too. But if you just see the
interface, there is no change visible for the user of the class (class
A). Just the calculation might be a bit faster. Besides I want the
class to be part of another one (class B). And I want B to have a
getter that returns a const reference to class A so that the user can't
do "stupid things".

Thomas


Wouldn't it be clearer to just check if the value was computed and if
not throw exception?

Jun 11 '06 #8

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

Similar topics

11
by: John D. Goulden | last post by:
Hopefully this question will make sense; if not, please correct my thinking :) If I wish to create a one-dimensional array at run time, I write int * myArray = new int ; and pass that array...
3
by: arut | last post by:
I would like to know when a const should be used and when a #define is necessary in a program using a constant. What are the pros and cons?
2
by: joegen | last post by:
Hi, I am working on an o project that involves Lazy Parsing. To be more specific, Its a SIP Message class that implements zero copy and lazy parsing. Below is a sample function that is causing...
5
by: Bit Byter | last post by:
I have a 'root' object that serves as a container/parent for several other objects like so: class myRoot { public: myRoot(); virtual ~myRoot(); void* operator new(size_t); operator...
12
by: Philip Potter | last post by:
I'm reading the comp.lang.c++ faq and, though it is incredibly clear and lucid in many points, I am completely confused by question 29.6: ...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
2
by: Andrew Chalk | last post by:
I have a variable that is initialized with a value read from a configuration file. Once initialized, it should not be changed anywhere in my program. What is the best way to enforce this (it should...
2
by: dillip132 | last post by:
Q--how can i know ,what are the methodes used in a claas...i want to retrieve all the methode used in my class.
11
by: Szabolcs Nagy | last post by:
is there a reason why const is not compile-time constant? the usual example, where it'd be nice is array declaration: const int N = 4; float arr or one may want to use an enum for indices:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.