assignment of member ' in read-only object

protected:
DWORD               m_dwMountTime;

public:
    DWORD GetLastHorseTime() const {
        return m_dwHorseTime;
    }


    DWORD CHARACTER::GetMyHorseVnum() const {
        int delta = 0;
        m_dwMountTime = get_dword_time();

        return c_aHorseStat[GetHorseLevel()].iNPCRace + delta;
    }

ok, there is my code.

After when i try to compile i get an error :

    char_horse.cpp: In member function 'virtual DWORD CHARACTER::GetMyHorseVnum() const':
    char_horse.cpp:210:16: error: assignment of member 'CHARACTER::m_dwHorseTime' in read-only object

  m_dwHorseTime = get_dword_time();

Where is the problem? :)

Answers


A member function marked const cannot change the state of the class. In GetMyHorseVnum() you have

m_dwMountTime = get_dword_time();

Which changes the state of the class. You either need to get rid of the const modifer on the function or make m_dwMountTime mutable


You declared your GetMyHorseVnum() method as const, so it cannot change (assigning) class members.


You can't change a member variable in a const member function, which promise won't change the state of the class.

You might want to make m_dwMountTime mutable to make it modifiable:

mutable DWORD m_dwMountTime;

mutable - applies to non-static class members of non-reference non-const type and specifies that the member does not affect the externally visible state of the class (as often used for mutexes, memo caches, lazy evaluation, and access instrumentation). mutable members of const classes are modifiable. (Note: the C++ language grammar treats mutable as a storage-class-specifier, but it does not affect storage class.)


Need Your Help

vb Writefile zip issue comes as a ashx

vb.net zip ashx

I have a ashx page handling file/attachment requests

Asp.Net MVC model validation with default text in textbox

asp.net-mvc model-validation

I am trying to validate input control in Asp.Net MVC. The below code checks if Project name is null and throws validation. I will have some default text in textbox on page load. I have a scenario to