Thread: Self Referencing (in a way)

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    26

    Self Referencing (in a way)

    Suppose in a class, I'd like to assign the address of the variable of that class type to a global variable, during construction. I don't know if this is possible, but it should, considering the address of the variable has already been assigned.

    Something like:
    Code:
    class() {
       globalvarptr = &(self?);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    this, not self. And since this is a pointer already, no need to take its address.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    I didn't understand that. There's a variable lets say 'a', which is off this class. While 'a' is being constructed, i'd like to the ptr to reference the address of a.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Inside any function of the class (including constructor and destructor) you can use pointer "this" to access the currect instance of the class
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    class little_bunny { little_bunny * foo_foo; little_bunny() : foo_foo(this) { hoppin_thru_da_forest(); } void hoppin_thru_da_forest() { std::cout << "hoppin_thru_da_forest"; } };

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Thanks, That's all I wanted.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Ok, I'm having another problem. The pointer that I'm going to reference to 'this' must be defined to be a 'class'. But that means that I'd have to put the class def infront of it. But the class includes this variable (globalvarptr), so it must be put before the class definition. How do I get out of this fix?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    The pointer that I'm going to reference to 'this' must be defined to be a 'class'.
    Must be defined as pointer to the class... you can do it before actual class definition
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Here's the code I used:
    Code:
    #include <iostream>
    using namespace std;
    
    ape* aptr;
    
    class ape
    {
    public:
    	ape(){
    		aptr =  this;
    	}
    	~ape(){}
    };
    
    
    int main()
    {
    
    	ape a;
    	cout << &a << " " << aptr;
    	return 0;
    }
    The complier errors are:
    1>d:\visual studio 2005\projects\test\test.cpp(4) : error C2143: syntax error : missing ';' before '*'
    1>d:\visual studio 2005\projects\test\test.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\visual studio 2005\projects\test\test.cpp(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\visual studio 2005\projects\test\test.cpp(10) : error C2440: '=' : cannot convert from 'ape *const ' to 'int *'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>d:\visual studio 2005\projects\test\test.cpp(19) : error C2146: syntax error : missing ';' before identifier 'a'
    1>d:\visual studio 2005\projects\test\test.cpp(19) : error C2065: 'a' : undeclared identifier
    I use the VC++ Express.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Oh, and another doubt, is there any call to output the type of the variable. Python has something like: type(variable), which outputs the type.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by studiesrule
    Code:
    ape* aptr;
    try
    Code:
    class ape* aptr;
    instead
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    worked like a charm. Thanks for your persistent help.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    don't forget to initialize the pointer to NULL to avoid using uninitialized pointer before the first instance of the class is created

    Don't forget to reset the pointer to NULL when the last instance is destructed.

    Don't forget to check that the pointer is not Null before using it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by vart
    don't forget to initialize the pointer to NULL to avoid using uninitialized pointer before the first instance of the class is created

    Don't forget to reset the pointer to NULL when the last instance is destructed.

    Don't forget to check that the pointer is not Null before using it...
    IOW use a smart pointer...
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    When you compile, check the intermediate assembler code. There is probably NO TRACE that you had EVER used classes at all. If you want get-instance-type sugary stuff, stick to Python.
    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. Symbol Referencing Error
    By Delpheno in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2008, 09:03 PM
  2. Symbol referencing errors
    By n00pster in forum C++ Programming
    Replies: 9
    Last Post: 04-30-2007, 06:40 AM
  3. STL list and referencing
    By gautamn in forum C++ Programming
    Replies: 1
    Last Post: 06-13-2005, 12:40 AM
  4. Problem in table referencing
    By elquex in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2003, 03:00 AM
  5. referencing structure variables
    By sballew in forum C Programming
    Replies: 1
    Last Post: 11-01-2001, 01:56 PM