Click here to Skip to main content
15,910,130 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralProblem with _beginthread Pin
Stephan Pilz16-May-04 21:57
Stephan Pilz16-May-04 21:57 
GeneralRe: Problem with _beginthread Pin
valikac17-May-04 6:54
valikac17-May-04 6:54 
GeneralRe: Problem with _beginthread Pin
Stephan Pilz17-May-04 20:06
Stephan Pilz17-May-04 20:06 
GeneralRe: Problem with _beginthread Pin
valikac17-May-04 20:16
valikac17-May-04 20:16 
GeneralA question for C++ gurus about exception handling Pin
Daniel Strigl16-May-04 21:13
Daniel Strigl16-May-04 21:13 
GeneralRe: A question for C++ gurus about exception handling Pin
Prakash Nadar16-May-04 21:39
Prakash Nadar16-May-04 21:39 
GeneralRe: A question for C++ gurus about exception handling Pin
Curi0us_George16-May-04 21:46
Curi0us_George16-May-04 21:46 
Generalconst function Pin
Jerome Conus16-May-04 20:53
Jerome Conus16-May-04 20:53 
Hi !

Here is a problem I'm not sure what would be the right way to solve it. I have a class which represents an operation and this operation has a duration. I want to be able to read the duration of the operation with the function getDuration(). As computing the duration is a long process, I don't want to do it before it is asked for, and I don't want to recompute it each time it is asked for. The code below is basically how I implemented it :

class Operation
{
public:
	Operation() { m_DurationCalculated=false;} ;
	~Operation();

	long getDuration()
	{
		computeDuration();
		return m_Duration;
	};

private:
	long m_Duration;
	bool m_DurationCalculated;

	void computeDuration()
	{
		if (!m_DurationCalculated)
		{
			// ...
			// Compute duration
			// ...
			m_Duration=/*...*/;
			m_DurationCalculated=true;
		}
	}
};


It works fine except that I want now the function getDuration() to be const, because, from an external point of view, getDuration() doesn't change the operation.
The problem, when making getDuration() const, is that it calls computeDuration(), which, I guess, should be const as well, and as m_Duration and m_DurationCalculated might be changed in a const function, I guess I have to put the mutable keyword in front of these member variables.
This is where I'm not sure : is it the correct way to solve this ? It looks strange to me that computeDuration, which really is not a const function, should be const in order to be called by the getDuration function.

What is the correct way to solve my problem ?

Thanks for your help !
Jerome
GeneralRe: const function Pin
nguyenvhn16-May-04 21:12
nguyenvhn16-May-04 21:12 
GeneralRe: const function Pin
Jerome Conus16-May-04 21:32
Jerome Conus16-May-04 21:32 
GeneralRe: const function Pin
Curi0us_George16-May-04 21:56
Curi0us_George16-May-04 21:56 
GeneralRe: const function Pin
Jerome Conus16-May-04 22:51
Jerome Conus16-May-04 22:51 
GeneralRe: const function Pin
Curi0us_George16-May-04 23:39
Curi0us_George16-May-04 23:39 
GeneralRe: const function Pin
nguyenvhn16-May-04 22:53
nguyenvhn16-May-04 22:53 
GeneralRe: const function Pin
Michael Dunn17-May-04 0:41
sitebuilderMichael Dunn17-May-04 0:41 
GeneralRe: const function Pin
Antony M Kancidrowski17-May-04 1:18
Antony M Kancidrowski17-May-04 1:18 
GeneralJPG Image In Picture Control Pin
balajeedurai16-May-04 19:56
balajeedurai16-May-04 19:56 
GeneralRe: JPG Image In Picture Control Pin
ThatsAlok3-May-05 23:20
ThatsAlok3-May-05 23:20 
GeneralProblem with CBitmapButton Pin
lostris16-May-04 19:52
lostris16-May-04 19:52 
GeneralRe: Problem with CBitmapButton Pin
David Crow17-May-04 2:45
David Crow17-May-04 2:45 
GeneralData synchronisation Pin
Ryan Binns16-May-04 19:15
Ryan Binns16-May-04 19:15 
General48*48 icon Pin
Imtiaz Murtaza16-May-04 19:04
Imtiaz Murtaza16-May-04 19:04 
GeneralRe: 48*48 icon Pin
toxcct16-May-04 22:14
toxcct16-May-04 22:14 
GeneralIMPLEMENT_DYNCREATE Pin
alex.barylski16-May-04 18:42
alex.barylski16-May-04 18:42 
GeneralRe: IMPLEMENT_DYNCREATE Pin
Ryan Binns16-May-04 18:45
Ryan Binns16-May-04 18:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.