Hello. This is hard to explain and to make it easy to understand it came a bit long.
Look at this example:
1 2 3 4
|
GString myObj = "he";
myObj.Append("llo");
cout << myObj; // operator<< overloaded for GString class
|
The Append function just concatenates one GString with a char*
|
void Append(const char* toAdd);
|
This is the
output of the program (debug couts included):
1 2
|
Constructor for (he) invoked
hello
|
The GString (implicit) constructor takes a char* to initialize myObj
Then we concatenate myObj with another char* ("llo") using the Append() function
All fine up until now... but look what happens with this code:
1 2
|
GString my = GString("h") + "llo";
cout << my;
|
The output? A mess:
1 2 3 4 5 6 7 8
|
Constructor for (h) invoked
Constructor for ( #nullptr#) invoked
Destructor for (h) invoked
( #nullptr# )
|
Alright, what the f**k is that "#nullptr#", you may ask.
Since I really mess up sometimes, I find really useful to use
couts to Debug my code when running.
And... YES! That "#nullptr#" string belongs to my cout!
Look
VERY CAREFULLY at the Constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
GString::GString(const char* CstyleString)
{
// USE_DEBUG is a macro that I set either to true or false to enable/disable printing
if (USE_DEBUG)
{
// here, round brackets encase the CStyleString
if (CstyleString)
cout << "Constructor for (" << CstyleString << ")" << " invoked";
// here, the "#nullptr#" string is printed out directly, without round brackets ( )
else
cout << "Constructor for #nullptr#" << endl;
}
// builds the string with dynamic allocation ecc...
Create(CstyleString);
}
|
First point:
"llo" is a temporary and calls the Constructor.
In the Constructor, CstyleString's value should have been "llo"
Intead, I get that strange result
Second point:
In the second cout, I get this (look carefully):
Constructor for ( #nullptr#) invoked
This tells me the Constructor has been invoked, but CstyleString's value is not what I wanted.
NOW READ CAREFULLY:
Had CstyleString value been 'nullptr', then it would have printed just "#nullptr#"
WITHOUT ROUND BRACKETS
Instead,
I SEE A MIX!: I have "#nullptr# encased between two round brackets???
JUST... WHAT THE F!?!??!
************ WHAT IF I DISABLE THE "USE_DEBUG" MACRO? ************
Well... this is the output:
*********************************************************
EDIT:
With the Visual Studio Debugger I noticed that when the Constructor for "llo" is called
CstyleString value is " #nullptr#"
My question is HOW THE HELL did that value go there?
EDIT 2:
If I completely comment the couts inside the Constructor I get
ed for (
It's a chopped string from the ~Destructor
*********************************************************
Guys, I know it's boring but please if you have any tips about what the heck is happening I'd be glad to hear!
For the complete implementation of the GString class, here is the link!
Really, if you have time, take a look at it
https://github.com/gedamial/GedamialLibrary/tree/master/gedamial/Data