Thread: How do I make a class call it's own address

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    49

    How do I make a class call it's own address

    I would like to make a class and have it store it's own memory address as a private data member automatically when it is created. Additionally I would like to make a function within that same class which, when called on, can determine it's memory address and store it in the private date member. Is there a way to do this?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe something like this?
    Code:
    #include <iostream>
    
    class T
    {
       T *address;
       void (T::*mypfoo)();
    public:
       T() : address(this)
       {
          std::cout << "address = " << address << '\n';
       }
       void foo()
       {
          mypfoo = &T::foo;
       }
    };
    
    int main()
    {
       T a, b, c;
       a.foo();
       b.foo();
       c.foo();
       return 0;
    }
    
    /* my input/output
    address = 0012FF88
    address = 0012FF84
    address = 0012FF80
    */
    http://www.parashift.com/c++-faq-lit...o-members.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    My understanding is that you just want the memory address of the class instance, not of the member functions. In that case it is already stored (no need for a private variable). The keyword 'this' is a pointer to the current object instance, and you can use it as the address of the instance.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    As I understand, C++ simulates reference passing with pointers. When you call member functions of a class, you are essentially calling a global function with the particular instance of the class passed by reference as an argument.

    There is also a way to get the addresses of member functions, if that's what you want. (Function pointers.) I think you can only do that outside of the member function, though. However, you can try to create a function object with a constructor (to get its address) and oerloaded operator(), and use a static instance for each class. The syntax might be a little convoluted, so you should have a very good reason for wanting to do this.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  2. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  3. Trouble with Call by Address
    By Vireyda in forum C++ Programming
    Replies: 13
    Last Post: 04-10-2004, 10:57 PM
  4. Replies: 12
    Last Post: 05-17-2003, 05:58 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM