Operator overloading

Pages: 123
Thanks.
Yes, after reading the early part of my new book I started going back to my code to add all the variations to see what the compiler might be finicky about.
1
2
3
4
const char* pChar{ "Hello" };     //Preferred new way
const char* pChar = "Hello";
const char* pChar = ("Hello");
const char* pChar ("Hello");



My original book is very good with constness for pointers, let me practice it here without looking.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Can change the char in the string literal and where the pointer is pointing to
//Should not use in our case, since it may allow us to change string literal
char* pChar{ "Hello" };         

//Constant pointer
//Shouldn't use if plan on using the pointer & not fixing it, and bad for being able to modify the char
char* const pChar{ "Hello" };

//A constant char to a constant pointer
//Bad for us here since we want to move the pointer around
const char* const pChar{ "Hello" };

//Constant char to a pointer
//Perfect!
const char* pChar{ "Hello" };



Sorry I was not clear. Yes, I know that NULL ('\0') and decimal zero are different ASCII values and I was trying to give my thought process. I need/should return a value for when (index >= GetLength()). So I return NULL but I don't really want to print the NULL since it looks too closely to a zero. Or on my screen looks exactly like a zero. I said differentiation issues, as in someone not being able to differentiate it on the screen.I also know that NULL can be checked against in code.

Imagine in another case going beyond the bounds of an array and printing out NULL, which looks too close to a zero, and the person thinks they have just lost all their investment. They may be inspired to jump out the window.

So I can use an extended ASCII character instead & print it out. Or I can still use the NULL return, but in main do a check == NULL and skip the character print but still have the message warning string print out. Which I think is good enough.

1
2
3
4
5
6
	//Force '\0' lookup, dangerous I know!
	cout << (unsigned int)pChar[strlen(pChar) + 1] << endl;	//0
	cout << NULL << endl;
	cout << (unsigned int)(char)0 << endl;
	cout << (char)48 << endl;
	cout << 0 << endl;

OUTPUT:
0
0
0
0
0

I did some more reading on "const char*" on string literals after this post. Some compilers might let one change the characters in the literal because of the way it was designed with backward compatibility with C, even though it is a "const char*".

So the "const char*" stores the string literal "like" an array of const chars with a NULL '\0' terminator. So it is "similar" to a char array, but not exactly in that it loses its ability to report back sizeof(). My guess is that it is not "exactly" like a char array because they wanted the pointer to remain light and speedy?

I also read some comments in other post that say you should NEVER check for the size for this and that it does not exist. Just don't do it, some proclaim! But you really don't have to if you know how it works behind the scene with all these particular details and since one can get it with "strlen(myString) + 1" and that will be the size ALWAYS when checked, right? But you guys surprise me sometimes.

Oh yea, that looks like a juicy read after I do more reviews, thanks!
https://www.cppstories.com/2024/constexpr-number-parsing-cpp23/

Good catch on the int if you want to be really disciplined, but it is from the book and probably just quick for code samples. I am sure there must be tons of operators out there that use an int with no need for neg nums, you guys tell me?
Last edited on
SubZeroWins wrote:
1
2
3
//Can change the char in the string literal and where the pointer is pointing to
//Should not use in our case, since it may allow us to change string literal
char* pChar{ "Hello" };

This is not allowed since C++11. The compiler should give you at least a warning about this code.

SubZeroWins wrote:
Some compilers might let one change the characters in the literal because of the way it was designed with backward compatibility with C

Even if the compiler let you use char* without const to point at a string literal it doesn't mean you're allowed to modify it. Sure the compiler won't stop you but if you do it's still UB and might crash your program or lead to other problems such as the string literal being wrong the next time you use it.

SubZeroWins wrote:
NULL

Please don't write NULL when talking about the null character '\0'.

NULL is a macro that is intended to be used as a null pointer constant. It might not even be implicitly convertible to char.
https://en.cppreference.com/w/cpp/types/NULL

SubZeroWins wrote:
I need/should return a value for when (index >= GetLength()).

Another option is to throw an exception. Then you don't have to return anything.

SubZeroWins wrote:
So I return NULL but I don't really want to print the NULL since it looks too closely to a zero. Or on my screen looks exactly like a zero.

Make sure you're printing the null character and not the NULL macro.

The null character isn't really meant to be printed so you might not see anything at all.

Returning '?' might make most sense when printing.
Last edited on
Oh, I thought they were the same thing. A null terminated character = '\0', and I thought when you NULL a pointer it sets it to '\0'.

I have to investigate this then, thanks!!!
Registered users can post here. Sign in or register to post.
Pages: 123