c++ - Subscripting a reference to const -
i'm here looking @ c++ code , not understanding something. irrelevant comes yarp (robot middleware) tutorial goes documentation.
virtual void getheader(const bytes& header) { const char *target = "humanity"; (int i=0; i<8 && i<header.length(); i++) { header.get()[i] = target[i]; } } now, header reference const , cannot modified within function. get called on it, prototype char *get() const;. how can header.get() subscripted , modified ? program compiles fine. may have not understood happens here i'm basing myself on i've read in c++ primer...
i appreciate little clarification!
have nice day,
char *get() const; the right hand const means "this member doesn't alter in class that's not mutable", , it's honoring - isn't changing anything. implementation this:
char *bytes::get() const { return const_cast<char *>(m_bytes); } the pointer being returned, however, simple "char*". think of way:
(header.get())[i] = target[i]; // or char* p = header.get(); p[i] = target[i];
Comments
Post a Comment