449,026 Members | 975 Online
Need help? Post your question and get tips & solutions from a community of 449,026 IT Pros & Developers. It's quick & easy.

# Logical Constness

 P: n/a 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 Replies

 P: n/a On Jun 25, 2:40 pm, Tarique

 P: n/a SeanW wrote: On Jun 25, 2:40 pm, Tarique Can someone please explain the idea of Logical Constness as explained insection 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with ashort 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

 P: n/a Tarique

 P: n/a On 2008-06-26 08:25:33, Tarique wrote: SeanW wrote: >On Jun 25, 2:40 pm, Tarique >Can someone please explain the idea of Logical Constness as explained insection 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with ashort working code if possible.Thank You This article covers it: http://www.ddj.com/cpp/184403892Sean 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

 P: n/a Pascal J. Bourguignon wrote: Tarique Can someone please explain the idea of Logical Constness as explainedin 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

 P: n/a 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 #include 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(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<

### This discussion thread is closed

Replies have been disabled for this discussion.