The following code does not compile, saying βerror C2248:β A :: getMe β: cannot access the private member declared in classβ A. βWhy? I'm trying to call the public interface.
class B { }; class A { public: const B& getMe() const; private: B& getMe(); }; int main() { A a; const B& b = a.getMe(); return 0; }
, , , ++. B& A::getMe() main, . , a.getMe() , B& A::getMe() B const& A::getMe() const. a const, . , . private non const member, const , const, non const.
B& A::getMe()
main
a.getMe()
B const& A::getMe() const
a
const
, ββ, : , . , , . .
: , , .
++ . - : , , .
, , const_cast :
const_cast
const B& b = const_cast<const A&>(a).getMe();
, , , :
const A& const_a = a; const B& b = const_a.getMe();
a const a.getMe(), , const- .
A const a; const B& b = a.getMe();
, ( , A).
, const non const a const, non const.
It would not be easier to call the private memeber function something else to avoid confusion in the presence of two functions with the same name but different access.
Source: https://habr.com/ru/post/1716572/More articles:IE8: the option tag gets the default attribute "selected" (And cloneNode makes a mess) - javascriptDrag and Drop in WPF - wpfEmpty popup in SharePoint - internet-explorerwhere msscript.ocx is installed from - scriptingWeblogic context search error: java.rmi.UnmarshalException: error arguments - javahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1716573/ddd-repositories&usg=ALkJrhg1bRaCAo-xG-HOEDVitEWb6X2jFAjava.util.Properties and Cp1250 - javaSilverlight: defining an event handler in a hierarchical data template - eventsConvert NSNumber to CLCoordinate - objective-cGet username even if Windows Authentication - c #All Articles