Code Example - mutable keyword
A example using the mutable keyword. Idea taken from the book "The C++ Programming Language" by Bjarne Stroustrup
AI
ملخص الذكاء الاصطناعي: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
كود المصدر
/* Code Example - mutable keyword
written by Jared Bruni
www.LostSideDead.com
"Open Source, Open Mind"
*/
#include<iostream>
#include<string>
using namespace std;
class Date {
int m,d,y;
mutable bool cache_there;
mutable string cache;
public:
Date();
Date(int mm, int dd, int yy);
void setdate(int mm, int dd, int yy);
int getday() const;
int getmonth() const;
int getyear() const;
void setcache() const;
string cpp_str() const;
};
Date::Date()
{
m = 0;
d = 0;
y = 0;
}
Date::Date(int mm, int dd, int yy)
{
m = mm;
y = yy;
d = dd;
}
void Date::setdate(int mm, int dd, int yy)
{
m = mm;
d = dd;
y = yy;
cache_there = false;
cache = "";
}
int Date::getmonth() const
{
return m;
}
int Date::getyear() const
{
return y;
}
int Date::getday() const
{
return d;
}
void Date::setcache() const
{
char mm[25];
char dd[25];
char yy[25];
itoa(m,mm,10);
itoa(d,dd,10);
itoa(y,yy,10);
cache = "";
cache += mm;
cache += "/";
cache += dd;
cache += "/";
cache += yy;
}
string Date::cpp_str() const
{
if(cache_there == false)
{
setcache();
cache_there = true;
}
return cache;
}
int main()
{
Date date;
date.setdate(1,12,2002);
cout << date.cpp_str() << endl;
system("pause");
return (0);
}
التعليقات الأصلية (3)
مسترجع من Wayback Machine