We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

A const Problem

Dj_Fx8
Dj_Fx8 asked
on
Medium Priority
339 Views
Last Modified: 2013-11-20
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
Comment
Watch Question

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.

CERTIFIED EXPERT

Commented:
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.

Commented:
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...
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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 :-?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.