You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, June 5th 2004, 10:47am

What difference are there between these codes?

First code:
int MyLayout::heightForWidth( int w ) const
{
if ( cache_dirty || cached_width != w ) {
// not all C++ compilers support "mutable"
MyLayout *that = (MyLayout*)this;
int h = calculateHeightForWidth( w );
that->cached_hfw = h;
return h;

}
return cached_hfw;
}

Second code:
int MyLayout::heightForWidth( int w ) const
{
if ( cache_dirty || cached_width != w ) {
// not all C++ compilers support "mutable"
cached_hfw = calculateHeightForWidth( w );
}
return cached_hfw;
}


other question: What does "mutable" mean?
Is the red part of First code nessary?
if yes, why? if not,What is the advantage?

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

2

Saturday, June 5th 2004, 12:32pm

RE: What difference are there between these codes?

Quoted

int MyLayout::heightForWidth( int w ) const
{
if ( cache_dirty || cached_width != w ) {
// not all C++ compilers support "mutable"
MyLayout *that = (MyLayout*)this;
int h = calculateHeightForWidth( w );
that->cached_hfw = h;
return h;

}
return cached_hfw;
}

You get HeightForWidth() and you cahe it in another object instance.

Quoted

int MyLayout::heightForWidth( int w ) const
{
if ( cache_dirty || cached_width != w ) {
// not all C++ compilers support "mutable"
cached_hfw = calculateHeightForWidth( w );
}
return cached_hfw;
}

You store the result in the same object instance.

Quoted


other question: What does "mutable" mean?

You can get some comprehensive doc at C++ Tutorial

Quoted

Is the red part of First code nessary?
if yes, why? if not,What is the advantage?

See above.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

hgy

Beginner

  • "hgy" is male

Posts: 33

Location: Switzerland

  • Send private message

3

Saturday, June 5th 2004, 9:09pm

RE: What difference are there between these codes?

what mutable means:

A method declared "const" must not modify any member variables or call non const methods of the class.
You can declare a member variable as "mutable" to allow a const method to modify it. This is typically used for cached values.

If a compiler does not support mutable, you can not modify members in const methods.
The trick with casting "this" to "that" is more a hack than a workaroud. It is to pretend that "that" is another object - but obviously it is the same as "this".

Hanspeter

4

Monday, June 7th 2004, 2:36am

RE: What difference are there between these codes?

In the second code, can I get the HeightForWidth()? (I mean I store and return it)

Why is the HeightForWidth() cached in another object? (I mean if I want to return it , I must do that?)

This post has been edited 1 times, last edit by "dr_super" (Jun 7th 2004, 2:37am)


djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

5

Monday, June 7th 2004, 5:23am

RE: What difference are there between these codes?

Quoted

Originally posted by dr_super
In the second code, can I get the HeightForWidth()? (I mean I store and return it)

Why is the HeightForWidth() cached in another object? (I mean if I want to return it , I must do that?)


using that-> instead of direct is a hack if cached_hfw has not been declared mutable. Remember, a const method cannot change members, so, in your case:

1. compiler supports mutable. Use it.
or:
2. use that as a trick to allow changing value. that is a virtual other object, a trick to make the compiler believe you dont' change this.
3. If the method is not part of a property, remove the const declaration.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

6

Monday, June 7th 2004, 8:59am

RE: What difference are there between these codes?

Thanks very much!