Advertisement
2002ASP Miscellaneous #7703

Code Example - mutable keyword

A example using the mutable keyword. Idea taken from the book "The C++ Programming Language" by Bjarne Stroustrup

AI

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.

소스 코드
original-source
/* 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에서 복구됨