Dj_Fx8
asked on
A const Problem
Hi
I have 2 functions declared in a class CCueSheet
int SongPercentage(const int TrackNum)const;
int GetTrackStart(const int nTrackNum, CString* pszStartTime = NULL);
Their definitation are
int CCueSheet::SongPercentage( const int TrackNum)const
{
int nStart = GetTrackStart(TrackNum);
return ((1000000/GetDurationSecs( );) * nStart) / 10000;
}
int CCueSheet::GetTrackStart(c onst int nTrackNum, CString* pszStartTime)
{
if (nTrackNum > m_naStartTimes.GetUpperBou nd())
return -1;
int nStart = m_naStartTimes.GetAt(nTrac kNum);
if (pszStartTime != NULL) {
*pszStartTime = CCommonFunctions::SecondsT oHMSmS(nSt art);
*pszStartTime = pszStartTime->Left(8);
}
return nStart;
}
Now when I try to compile this I get the following error
error C2662: 'GetTrackStart' : cannot convert 'this' pointer from 'const class CCueSheet' to 'class CCueSheet &'
Conversion loses qualifiers
But if I declare the SongPercentage function as
int SongPercentage(const int TrackNum);
It works fine, why is this
I have 2 functions declared in a class CCueSheet
int SongPercentage(const int TrackNum)const;
int GetTrackStart(const int nTrackNum, CString* pszStartTime = NULL);
Their definitation are
int CCueSheet::SongPercentage(
{
int nStart = GetTrackStart(TrackNum);
return ((1000000/GetDurationSecs(
}
int CCueSheet::GetTrackStart(c
{
if (nTrackNum > m_naStartTimes.GetUpperBou
return -1;
int nStart = m_naStartTimes.GetAt(nTrac
if (pszStartTime != NULL) {
*pszStartTime = CCommonFunctions::SecondsT
*pszStartTime = pszStartTime->Left(8);
}
return nStart;
}
Now when I try to compile this I get the following error
error C2662: 'GetTrackStart' : cannot convert 'this' pointer from 'const class CCueSheet' to 'class CCueSheet &'
Conversion loses qualifiers
But if I declare the SongPercentage function as
int SongPercentage(const int TrackNum);
It works fine, why is this
bkfirebird is right, the const method is causing the problem because you are calling a non-const method.
There is one way to modify a member variable in a const method: If you declare the variable as "mutable" it can be modified. I think it's bad programming style, but it's in the C++ standard.
There is one way to modify a member variable in a const method: If you declare the variable as "mutable" it can be modified. I think it's bad programming style, but it's in the C++ standard.
try this
in .h
class CCueSheet{
...
int GetTrackStart(const int nTrackNum, CString* pszStartTime) const;
};
in .cpp
int CCueSheet::GetTrackStart(c onst int nTrackNum, CString* pszStartTime) const;
Of cause as prev. poster pointed out you won't be able to modfy any varables withing this class, but from the look of it you don't do it anyway.
Hope it helps...
in .h
class CCueSheet{
...
int GetTrackStart(const int nTrackNum, CString* pszStartTime) const;
};
in .cpp
int CCueSheet::GetTrackStart(c
Of cause as prev. poster pointed out you won't be able to modfy any varables withing this class, but from the look of it you don't do it anyway.
Hope it helps...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
Sorry for the delay, I got side tracked, having quickly looked over the comments provided ys and vijay gave what I feel as the simplest and most straight forward answers, so I split the points between them, thanks for all your replies.
Sometimes when self teaching you miss little basic things which come back to haunt you later :-?
Sorry for the delay, I got side tracked, having quickly looked over the comments provided ys and vijay gave what I feel as the simplest and most straight forward answers, so I split the points between them, thanks for all your replies.
Sometimes when self teaching you miss little basic things which come back to haunt you later :-?
When you call a const function on a non-const object the compiler won't allow it. The object will be passed as the 'this' pointer to the function. The 'this' will be non-const but the function will expect a const 'this' pointer. Hence the error message.
Member functions that modify the object would be non-const and functions that return data but don't modify anything are normally declared const.