Thread: Overloading a pointer to a class

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Overloading a pointer to a class

    I don't think it's possible, but can I overload a pointer to a class? Ie:
    Code:
    class M
    {
    public:
        M *operator *= (M *);
    };
    
    M *M::operator *= (M *param)
    {
        //Do things by dereferencing
        return this;
    }
    
    void func(M *a,M *b)
    {
        a*=b; //This would give characteristics of b to a
    }
    The operation I want performed has nothing to do with multiplication - it's just a random pick of operator. So, is this possible? I tried it like the above, without success, but maybe I have the syntax wrong?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    No. A pointer is a pointer. Not an object.

    gg

    [EDIT]
    Using "->" or "*" on a pointer will always dereference (ie. not call a method). Otherwise, it's just a 32 (or 64) bit value.
    Last edited by Codeplug; 10-27-2003 at 07:24 AM.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Ok then, let's say I overload the class (and I did). I'm now doing this:
    Code:
    void Func(M *a,M *b)
    {
        (*a) *= (*b);
    }
    Is that the best way to manipulate the pointers?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You could use references:

    Code:
    void Func(M &a, M &b)
    {
        a *= b;
    }
    gg

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Ok, cheers, I should be alright with that for now.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM