operator >> overloading

josh BAUER 166 Reputation points
2020-11-18T20:51:09.273+00:00

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?

include <iostream>

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;
}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,693 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 116.7K Reputation points
    2020-11-18T21:14:02.34+00:00

    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;
    . . .
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Barry Schwarz 2,831 Reputation points
    2020-11-18T21:23:50.57+00:00

    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.

    0 comments No comments

  2. Darran Rowe 916 Reputation points
    2020-11-18T21:31:32.967+00:00

    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.

    0 comments No comments

  3. josh BAUER 166 Reputation points
    2020-11-18T21:34:22.7+00:00

    Thanks. How can I pay you?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.