Check this specific implementation:
console& operator>>( console& tempConsole, const char* &tempChars )
{
static const char* returnTHIS = "ll";
tempChars = returnTHIS;
return tempConsole;
}
. . .
const char* pp = NULL;
cons >> pp;
. . .
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I need to locate char pointer in variable pp but in debugger it shows me all time that
to operator>> NULL is given but not address of pp.
HOW to locate pointer to returnTHIS into variable pp via tempChars?
static class console {
public:
friend console& operator<<(console& os, const char* tempChars);
friend console& operator>>(console& os, const char& tempChars);
};
console& operator<<(console& tempConsole, const char* tempChars)
{
std::cout << tempChars;
return tempConsole;
}
console& operator>>(console& tempConsole, const char* tempChars)
{
char* returnTHIS = new char[] {'l','l'};
tempChars = returnTHIS; //this fails TO WORK OUT
return tempConsole;
}
int main()
{
console cons;
char* pp = NULL;
cons >> pp;//need to get pointer to chars
cons << pp;
}
Check this specific implementation:
console& operator>>( console& tempConsole, const char* &tempChars )
{
static const char* returnTHIS = "ll";
tempChars = returnTHIS;
return tempConsole;
}
. . .
const char* pp = NULL;
cons >> pp;
. . .
You set pp to NULL.
You call your >> function. Parameters are passed by value. The value NULL is copied into the function local variable tempChars. returnTHIS points to the newly allocated memory. The current value (NULL) in tempChars is replaced by the value in returnTHIS. Your function returns. tempChars is destroyed as is normal for local function variables when they go out of scope.
Notice that nothing happened to pp. It still contains the value NULL.
If you change the function signature to accept the char* by reference, that may solve your problem since tempChars essentially becomes an alias for pp.
Notice also that you never release the memory allocated by new. If you call your >> function more than once, you have created a probable memory leak. Also consider that there is no need for your char[] to by dynamically allocated. You could change the definition of returnTHIS to
static const char returnTHIS[] = {'|', '|'};
After these changes, pp would point to the static array inside your >> function and your << function would have no trouble accessing it.
There are several things wrong with this code.
First, the parameters for operator>> do not match the friend definition in the class. The parameters need to match EXACTLY.
Second, the parameter is one way, so the modification inside the function will not affect the pp variable in the main function. For this to work, the parameter for operator>> must be a reference to a pointer to char. (char * (&tempChars)). It can't me const because you are modifying it.
Third, the new char[] {'l', 'l'} statement will not initialise the string.
Fourth, you have a new without delete, so this will leak memory.
Thanks. How can I pay you?