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

Logical Constness

Can someone please explain the idea of Logical Constness as explained in
section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
short working code if possible.

Thank You
Jun 27 '08 #1
6 2444
On Jun 25, 2:40 pm, Tarique <peo_...@yahoo.comwrote:
Can someone please explain the idea of Logical Constness as explained in
section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
short working code if possible.

Thank You
This article covers it: http://www.ddj.com/cpp/184403892

Sean
Jun 27 '08 #2
SeanW wrote:
On Jun 25, 2:40 pm, Tarique <peo_...@yahoo.comwrote:
>Can someone please explain the idea of Logical Constness as explained in
section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
short working code if possible.

Thank You

This article covers it: http://www.ddj.com/cpp/184403892

Sean
I found this link when i Google'd it and i could not make much sense of
it! Phew!!! I am only a C++ newbie. Maybe i should have mentioned that
in my original post.

I am only looking for a very short explanation if possible!

Thanks again.
Jun 27 '08 #3
Tarique <pe*****@yahoo.comwrites:
Can someone please explain the idea of Logical Constness as explained
in section 10.2.7.1 Of Stroustrup's "The C++ Programming Language"
,with a short working code if possible.
Assume you have an object that has a very big vector of integers, and
that provides a method to get the sum the integers in this very
vector. Since computing this sum doesn't change the vector nor the
sum, it's a method that is logicall const. But if you try to declare
it formally const, then you cannot do the obvious optimization of
caching this sum (or you need to declare mutable the sum and the flag
that indicate that it's up-to-date).
class Strange {
protected:
std::vector<floatv(1000000);
public:
float getSum(void) const;
void set(int index,float value);
};

void Strange::set(int index,float value){
v[index]=value;
}

float Strange::getSum(void) const {
struct Summer{
float sum;
Summer():sum(0.0){}
void sum(float incr){ sum+=incr; }
};
Summer s();
for_each(v.begin(),v.end(),s); // very slow
return(s.sum);
}
So instead, we could lose the const, and while the method getSum is
still logically const, it can modify the object:
class Strange {
protected:
std::vector<floatv(1000000);
bool changed;
float sumCache;
public:
Strange():changed(true){}
float getSum(void) /* logically const */;
void set(int index,float value);
};

void Strange::set(int index,float value){
v[index]=value;
changed=true;
}

float Strange::getSum(void) /* logically const */ {
if(changed){
struct Summer{
float sum;
Summer():sum(0.0){}
void sum(float incr){ sum+=incr; }
};
Summer s();
for_each(v.begin(),v.end(),s); // very slow
sumCache=s.sum;
}
return(sumCache);
}

Another example:

For a class of rationals, the normalization method doesn't change the
value of the rationnal (4/8 == 1/2) so it is "logically const", while
it still changes the numerator and denominator.

--
__Pascal Bourguignon__
Jun 27 '08 #4
On 2008-06-26 08:25:33, Tarique wrote:
SeanW wrote:
>On Jun 25, 2:40 pm, Tarique <peo_...@yahoo.comwrote:
>>Can someone please explain the idea of Logical Constness as explained in
section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
short working code if possible.

Thank You

This article covers it: http://www.ddj.com/cpp/184403892

Sean

I found this link when i Google'd it and i could not make much sense of
it! Phew!!! I am only a C++ newbie. Maybe i should have mentioned that
in my original post.

I am only looking for a very short explanation if possible!
Logical constness means that an object appears as constant and works as if
it were constant, but in reality some of the internal states do change.

Gerhard
Jun 27 '08 #5
Pascal J. Bourguignon wrote:
Tarique <pe*****@yahoo.comwrites:
>Can someone please explain the idea of Logical Constness as explained
in section 10.2.7.1 Of Stroustrup's "The C++ Programming Language"
,with a short working code if possible.

Assume you have an object that has a very big vector of integers, and
that provides a method to get the sum the integers in this very
vector. Since computing this sum doesn't change the vector nor the
sum, it's a method that is logicall const. But if you try to declare
it formally const, then you cannot do the obvious optimization of
caching this sum (or you need to declare mutable the sum and the flag
that indicate that it's up-to-date).
_snip_

>
Another example:

For a class of rationals, the normalization method doesn't change the
value of the rationnal (4/8 == 1/2) so it is "logically const", while
it still changes the numerator and denominator.
Thank You Sir.That really helped.
Jun 27 '08 #6
Tarique wrote:
Can someone please explain the idea of Logical Constness as explained in
section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
short working code if possible.

Thank You
From - Thu
I have tried implementing the example given in the same section (Naive
though and only to test the idea!)

#include<iostream>
#include<string>
using namespace std;

class Date {
int d,m,y;
bool cache_valid;
string cache;
void compute_cache_value();//fill cache
//..
public:
Date(int dd = 0,int mm = 0,int yy = 0); // Default constructor
static Date default_date;
~Date(){}; // Destructor

string string_rep()const;//string representation
Date& add_day (int n);
};

Date Date::default_date(03,01,2000);
Date::Date(int dd,int mm,int yy)
{
cache_valid = false;
d = dd ? dd : default_date.d;
m = mm ? mm : default_date.m;
y = yy ? yy : default_date.y;

}
void Date::compute_cache_value()
{
switch(d) //Test_Only
{
case 1:case 8:case 15:case 22:case 29:cache = "Monday"; break;
case 2:case 9:case 16:case 23:case 30:cache = "Tuesday"; break;
case 3:case 10:case 17:case 24:case 31:cache = Wednesday";break;
case 4:case 11:case 18:case 25:cache = "Thursday"; break;
case 5:case 12:case 19:case 26:cache = "Friday"; break;
case 6:case 13:case 20:case 27:cache = "Saturday"; break;
case 7:case 14:case 21:case 28:cache = "Sunday"; break;
}
}

string Date::string_rep() const
{
if(cache_valid == false) {
Date* th = const_cast<Date*>(this);//Cast away const
th->compute_cache_value();
th->cache_valid = true;
}
return cache;
}

/*
According to Stroustrups "The C++ Programming Language",
the const_cast operator is _not guaranteed to work_ when applied to an
object that was originally declared as const.
Why is it so ?
*/

Date& Date::add_day (int n) { //Without any bounds check!
d+=n; //I assume d remains < 31
cache_valid = false;
return *this;
}

int main()
{
Date d1;
const Date d2;
string s1=d1.string_rep();
cout<<s1<<endl;

d1.add_day(2);
cout<<d1.string_rep()<<endl;

string s2=d2.string_rep(); //Undefined Behaviour .Why ?
cout<<s2<<endl;
return 0;
}
Jun 29 '08 #7

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

Similar topics

1
by: Richard Hayden | last post by:
Hi, I understand such pointers as 'const int* const ip' and 'const int* ip' etc., but I'm getting confused when seeing things like 'const int* const* ip' (i.e. with two or more asterisks)....
15
by: Trevor Lango | last post by:
I want to be able to cast away the constness of a private member variable in a member function of a class. I have the private data member declared as follows: const double x; I have an...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
7
by: Olav | last post by:
Hi I wonder if it is possible to have templates expand based on constness, something like: template<typename A, typename B>copy( A &a, B &b); becoming: copy( const class AClass &a, class...
3
by: mailtogops | last post by:
Hi, I often think over the const_cast operator provided by C++ and I feel usage of const_cast or the document availale for const_cast are cumbersome. C++ provides us, const qualifier which...
3
by: LuB | last post by:
I'm writing a Win32 application - and more specifically, doing event programming. I want the application to be const compliant but I'm faced with a bit of a conundrum. Physically, many of my...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
2
by: Laurent Deniau | last post by:
I am looking for the "cleanest" way to cast away the constness of a pointee in C89, something like const_cast<T*>() in C++. Actually, I am using: #define CONST_CAST(typename,value) \ (((union {...
13
by: Javier | last post by:
Hello, I have some cuestions about constness with standard containers and pointers. Supose I have a list of pointers to some class B: std::list< B * list; I have readed that constness in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.