question of operator overloading on function pointer 
Author Message
 question of operator overloading on function pointer

Hi All,

I am trying my hand on operator overloading related to "cout<<endl;".
I write my own class as following but can't get operator << invoked on
my own endl. Could some anybody tell me what's wrong with my code.

I did it on VC6.0 starting with an empty console project with option
"Use MFC in a static libary"

Thanks in advance!!

Ray

#include <afx.h>

class COutObj  
{
public:
        COutObj():m_strOut(""){};
        virtual ~COutObj(){};

        COutObj & operator <<(CString str){
                m_strOut+=str;
                return *this;
        };
        COutObj & operator <<(COutObj& ( * f1)(COutObj& outs)){
                (*f1)(*this); return *this;
        }
        COutObj&  myendl(COutObj& outs){outs<<"myEndLine";return outs;}

private:
        CString m_strOut;

Quote:
};

void main(){
        COutObj mycout;
        mycout<<"aaaaaaaa";     //ok
        mycout<<myendl;           //error C2065: 'myendl' : undeclared identifier

        mycout<<COutObj::myendl;
                //error C2678: binary '<<' : no operator defined
                //which takes a left-hand operand of
                //type 'class COutObj' (or there is no acceptable conversion)

Quote:
}



Fri, 24 Sep 2004 15:44:49 GMT  
 question of operator overloading on function pointer

Quote:

> Hi All,

> I am trying my hand on operator overloading related to "cout<<endl;".
> I write my own class as following but can't get operator << invoked on
> my own endl. Could some anybody tell me what's wrong with my code.

> I did it on VC6.0 starting with an empty console project with option
> "Use MFC in a static libary"

> Thanks in advance!!

> Ray

> #include <afx.h>

> class COutObj  
> {
> public:
> COutObj():m_strOut(""){};
> virtual ~COutObj(){};

> COutObj & operator <<(CString str){
> m_strOut+=str;
> return *this;
> };
> COutObj & operator <<(COutObj& ( * f1)(COutObj& outs)){
> (*f1)(*this); return *this;
> }
> COutObj&  myendl(COutObj& outs){outs<<"myEndLine";return outs;}

> private:
> CString m_strOut;
> };

> void main(){
> COutObj mycout;
> mycout<<"aaaaaaaa"; file://ok
> mycout<<myendl; file://error C2065: 'myendl' : undeclared identifier

If you want this to compile you have to define myendl as a regular
function outside the class definition.

Quote:
> mycout<<COutObj::myendl;
>                 file://error C2678: binary '<<' : no operator defined
> file://which takes a left-hand operand of
> file://type 'class COutObj' (or there is no acceptable conversion)

You defined myendl as a non-static member function.
If you want this to compile you either have to define
it as a static member function

class COutObj  
{
public:
// ...
    static COutObj &  myendl(COutObj& outs) { outs << "myEndLine"; return outs; }

// ...

Quote:
};

or define another operator << taking a pointer-to-member

class COutObj  
{
public:
// ...
    COutObj & operator << (COutObj & (COutObj::*pmf) (COutObj& outs)) {
        (this->*pmf)(*this);
        return *this;
    }
// ...

Quote:
};

Sergei


Fri, 24 Sep 2004 16:22:13 GMT  
 question of operator overloading on function pointer
Now I got a clear mind. Thank you very much for your quick response. I
do appreciate for your time.

ray



Sat, 25 Sep 2004 13:55:21 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Function operator overloading

2. Template functions & overloaded operators

3. VC++ 5.0 ambiguity: conversion operator vs overloaded operator?

4. == and != operator (operator overloading)

5. Operator overloading question

6. Beginners question - Overloading the + operator in a string class

7. Question on Win32 memory management, overloaded assignment operator (=) and deep copying of a HANDLE

8. Operator overloading question

9. Question about signal()/pointers to functions that return pointers to functions

10. How to use (pointer to function), and function and pointer to (pointer to function)

11. Calling overloaded function in base from another overload in a derived class

12. Pointer Functions and Pointers to Pointer Functions

 

 
Powered by phpBB® Forum Software