Thread: operator overload

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    operator overload

    hi,
    i got some problem understand operator overload in C++
    Code:
    template <class T>
    class SShellPtr
    {
    public:
        ~SShellPtr ()
        {
            Free ();
            _malloc->Release ();
        }
        T * weak operator->() { return _p; }
        T const * operator->() const { return _p; }
        operator T const * () const { return _p; }    //what does this line mean??
        T const & GetAccess () const { return *_p; }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It declares an operator T const * for SShellPtr. No return type is required, as the return type is implied by the name of the operator. So, if x is an instance of SShellPtr<T>, then;
    Code:
       T const *px = x;
    or, alternatively,
    Code:
        T const *px = (T const *)x;
    or, even (as const applies to the thing on its left, unless it is leftmost, in which case it applies to the thing on its right)
    Code:
        const T *px = (const T *)x;
    In all these cases, the two lines;
    Code:
        px->SomeConstMemberFunctionOfT();
    and
    Code:
        x->SomeConstMemberFunctionOfT();
    have the same effect.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a type operator. If the compiler wants the type specified by the operator, it will call it.
    It's pretty nifty, since you can do a lot with it. So long as it's a valid type, it compiles fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed