Const methods to access private data members in C++ -
My project is here I have several other errors but I'm sure I'm not trying to redefine the string Was anything before asking it.
Invalid Player :: Exposure () const {cout & lt; & Lt; "\ N Player's Name:" & lt; & Lt; GetName () & lt; & Lt; "\ N Grades:" & lt; & Lt; GetGrade () & lt; & Lt; "\ NG.P.A." & Lt; & Lt; GetGPA () & lt; & Lt; Endl; }
The problem is here, I get access violation, this is a method when keeping display cnt or it is a valid code and my problem is elsewhere and it is not caught. Is Debugger?
// The types of well-changed gates are required to be changed; Player :: GetName () const {return m_name;} const int Player :: GetGrade () Const (Returns M_Grade;} Const Double Player :: GetGPA () const {return m_gpa;}
More relevant code, so I have learned that to read a private data member with a const method like this, you have to "promise" two constants to change privatization
Class player {public: four * gatnum () cons; Int Getgreat () Const .; DoubleGetPPA () Const .; Private: Four * m_name; Int m_grade; Double M_GPA; };
If you are not getting any compilation errors, then possible reasons four *
instead of const char *
to GetName ()
. If the name is only started with read-only memory (such as by using double quote marks such as "Tom"
) and you can see the pointer given by GetName ()
Try to write characters to get an access violation.
Comments
Post a Comment