Thread: Overloading dereferencing operator.

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Overloading dereferencing operator.

    I posted a question similar to this earlier called "I'm making C++". Does anyone know who I can overload * (the dereference operator, not the multiplication operator) so that I can gain access to the class on the right of it. It has a strange syntax: something = *something_else_in_a_pointer. Usually something + something else would be read as something.operator+(something else). So i just need to know what the function prototype would look like.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You can get at the right hand side with the use of friend functions.....

    #include <iostream>

    using std::cout;
    using std::endl;


    class tester{

    friend int operator*(const tester&); //declare friend

    public:
    tester::tester(int x){testno = x;} //constructor

    private:
    int testno; //private int (data)
    };

    int operator*(const tester& test){
    return test.testno ;

    }

    int main(void){

    tester test1(10);

    cout << "Number when overloading '*' = " << *test1 << endl;


    return 0;
    }
    That's just a quick mess around by me to see if I could get it to work...... and it seems to LOL

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    HEY THANKS! I've always wondered what the pirpose of friend functions was..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM