On 11/10/14, 8:04 AM,
ag...@drrob1.com wrote:
> typedef char* CharPointerType;
>
> Student(CharPointertype pName = "no name") : name(pName) IS
(I am presuming the capitalization difference is a typo)
First suggest, burn any book that told you to use all those macros to
make your code more "readable", they just make things incomprehensible
for those who really know the language.
The above two lines show the problem.
CharPointerType is a pointer to NON-CONST char, while string literals
are defined as arrays of const char. (In C, the type was array of
non-writable but not-const char for historical reasons).
Since the Student constructor doesn't modify the character string passed
to it, it should declare that it takes a pointer to const char, not a
pointer to char, and things will be fine. (Your original posting of code
did that).
The reason this was just a warning and not a full error is that like 25
years ago, before C had const (and C++ was just being born), since the
language didn't have const, string literals would be assigned to char*
pointers (and on some systems WERE writeable). When const was added to
the language, it was thought to make C string literals to be const, but
that would have broken too much code since the pointers they wee being
assigned to weren't const (but many tools were set to be able to give
warnings for this, and over time this has become more of the default, as
hopefully most of that ancient code has been fixed). C++ on the other
hand, since it didn't have that large code base, defined string literals
to be const, but many tools would have an option to make this just a
warning (or even ignored) to help with porting these older programs to C++.