Overloaded addressof operator?

say i have the following wrapper class

Class CHandle
{
public:
..
..
private:
HANDLE m_h;
};


how would i make an overloaded operator that will return the address of m_h member so that i can mimic the following api function

HANDLE h;
ReadFile(&h);

so that mine can be used like so..

CHandle h;
ReadFile(&h);
1
2
3
4
5
6
7
8
9
class CHandle
{
public:

    HANDLE* operator&() { return &m_h ; }

private:
    HANDLE m_h ;
};
> how would i make an overloaded operator that will return the address of m_h member

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct A
{
    int* operator& () { return &m_h ; }
    const int* operator& () const { return &m_h ; }

    private:
        char filler[123] ;
        int m_h ;
};

int main()
{
    A a ;
    std::cout << &a << ' ' << std::addressof(a) << '\n' ;
}


In this particular case, what would be more intuitive is:

1
2
3
4
5
6
7
8
9
Class CHandle
{  
    public:
        // ...
        operator HANDLE() { return m_h ; }
        // ...
    private:
        HANDLE m_h;
};


And then:
1
2
CHandle h( /* ... */ ) ;
ReadFile( h, /* ... */ ) ;
Also to inherit from HANDLE
1
2
3
4
5
6
class CHandle : HANDLE
{};

//...

ReadFile(&myCHandle);
Last edited on
Topic archived. No new replies allowed.