• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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(const int nTrackNum, CString* pszStartTime)
{
  if (nTrackNum > m_naStartTimes.GetUpperBound())
    return -1;

  int nStart = m_naStartTimes.GetAt(nTrackNum);
  if (pszStartTime != NULL)      {
    *pszStartTime = CCommonFunctions::SecondsToHMSmS(nStart);
    *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
0
Dj_Fx8
Asked:
Dj_Fx8
2 Solutions
 
bkfirebirdCommented:
Not too sure but when you specify a member function as being const it means it promises not to alter the member data of an object of that class. Also it means that you're saying you won't call any functions which aren't also specified as being const.

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.

0
 
Karl Heinz KremerCommented:
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.
0
 
mblatCommented:
try this

in .h

class CCueSheet{
...
int GetTrackStart(const int nTrackNum, CString* pszStartTime) const;
};

in .cpp
int CCueSheet::GetTrackStart(const 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...
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
_ys_Commented:
const member functions can only call other const member functions ...

Either declare them both as const (preferred as you don't actually changes any member variables) or declare them both as non-const.
0
 
vijay_visanaCommented:
I think your are missing very simple rule of c++ a const memberfunction can not call non-const member function. make GetTrackStart const function and error will not show up.

Vijay
0
 
Dj_Fx8Author Commented:
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 :-?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now