reference to self ... so to speak

This is a discussion on reference to self ... so to speak within the C++ Programming forums, part of the General Programming Boards category; Im just getting back into C and Im realizing how much I've forgotten (or didn't know) anyhow I'm trying to ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    Question reference to self ... so to speak

    Im just getting back into C and Im realizing how much I've forgotten (or didn't know)

    anyhow I'm trying to do some OOP. And I've got a class, where I want to define a pointer to the same class. That way I can make references to "siblings". Does anyone know what is the way to go about this?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    are you talking about the "this" pointer?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    wouldn't that be pointing to itself then?

    I want to point to the same type but a different instance

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    essentialy I want to do this
    Code:
    class myClass{
        
        myClass *pointer;
    
    }

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    So in essence you did it correctly... So what is the problem?

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by scorche View Post
    essentialy I want to do this
    Code:
    class myClass{
        
        myClass *pointer;
    
    }
    It's really just assigning

    pointer = this;

    But then you could just use "this" directly.

    edit:
    I think I really must be missing something in the question
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Quote Originally Posted by scorche View Post
    essentialy I want to do this
    Code:
    class myClass{
        
        myClass *pointer;
    
    }
    Sorry to burst your bubble, but:
    Code:
    class myClass
    {
        myClass* pointer;
    }
    This is a pointer to the same class type.

    Code:
    class myClass
    {
        myClass& reference;
    }
    This is a reference to the same class type.

    And this is C++, not C (big difference).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    he did say both pointer and reference in his post. So its pretty easy to assume he meant pointer here.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    I dislike the idea to use the word "reference" to something if you aren't actually using references because C++ sets them apart.
    And I wanted to fix that ugly bind-to-the-name style :)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    sorry, I've spent the last 2 years sonverting c++ practices to actionscript, and now I'm doing it the other way round...

    thanks, I'll give it a go

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    now I'm getting this error

    i switched to
    Code:
    class myClass
    {
        myClass& reference;
    }

    63 error: structure `_class' with uninitialized reference members

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Using a reference is harder than a pointer, because you have to initialize the reference in the constructor's initialization list. (And it also makes copying harder.)

    I'd just use a pointer and set it to null if you don't have an address to assign to it when you construct the object.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Elysia kind of has a point about this specific case (though I will argue with her about passing things by pointer/reference at times). You could make a reference....

    Example:
    Code:
    class MyBaseClass
    {
    private:
      int x, y;
    
    public:
      MyBaseClass() : x(0), y(0) { }
      virtual ~MyBaseClass() { }
    
      virtual void doSomething() = 0;
    
      int getX() const { return x; }
      int getY() const { return y; }
      void setX(int X) { x = X; }
      void setY(int Y) { y = Y; }
    };
    
    class MyOther : public MyBaseClass
    {
    protected:
      MyBaseClass &ref;
    
    public:
      MyOther(MyBaseClass &Ref) : ref(Ref) { }
      virtual ~MyOther() { }
    
      virtual void doSomething() { /* whatever */ }
    };
    Anyway, not the best example but you see the difference.

  14. #14
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    references have to be initialized when the reference is declared... probably not the best choice here as both the siblings would need to be instantiated together (and I can't really imagine a good way to do that)
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    "So in essence you did it correctly... So what is the problem?"

    I don't know... it gave me an error
    but I think it's ok now, must have done something silly...

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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