Overloading dereferencing operator.

This is a discussion on Overloading dereferencing operator. within the C++ Programming forums, part of the General Programming Boards category; I posted a question similar to this earlier called "I'm making C++". Does anyone know who I can overload * ...

  1. #1
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    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,789
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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, 01: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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21