Self-Referrential Classes

This is a discussion on Self-Referrential Classes within the C++ Programming forums, part of the General Programming Boards category; In an application I'm developing, I have a class with functions which refer to specific instances of the same class. ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Self-Referrential Classes

    In an application I'm developing, I have a class with functions which refer to specific instances of the same class.

    I'll post this example - the actual code is hideously long.

    Code:
    Class myClass {
    //never mind constructors and destructors, they aren't relevant here
    public:
    void SelfRefer();
    int Foo;
    
    };
    
    myClass Billy[2];
    
    void myClass::Selfrefer() {
    Billy[1].Foo=3;
    }
    As you can see, all instances of myClass refer to a specific instance of myClass (which I know will exist). The only way this code works is because I've put the instance Billy after the class itself but before the functions.

    I need to be able to declare instances of myClass in other places, like in the following example (which won't compile).

    Code:
    Class myClass {
    //never mind constructors and destructors, they aren't relevant here
    public:
    void SelfRefer();
    int Foo;
    
    };
    
    void myClass::Selfrefer() {
    Billy[1].Foo=3;
    }
    
    myClass Billy[2];
    What can I do? The instances can't be declared before the class itself is, but if they're declared afterwards they won't "exist yet" in the eyes of the class's Selfrefer() function, producing an error (along the lines of "Billy[] not declared" or something).

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Perhaps extern would help.

    Or you could make your functions take a myClass object by reference, and then just pass the appropriate instance when you call the function. That global instance might not be the best design choice.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    But... if you want to reference an object of the same type, why not create myClass::SelfRefer(myClass& obj)?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    Quote Originally Posted by Daved
    Perhaps extern would help.

    Or you could make your functions take a myClass object by reference, and then just pass the appropriate instance when you call the function. That global instance might not be the best design choice.
    Extern can't really be applied here as far as I know, the compiler still wonders what Billy[] is when it's compiling Selfrefer().

    I'm working on a reference (or possibly a pointer) solution now, good thinking (no idea if it's going to work though).

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    One of the things the compiler provides for you when you make a class is the copy constructor. You need to write one of your own if you have data members on the heap or anything beyond plain old data as members.

    Say you had an array of People:
    Person crowd[3];
    And you create Person 1.
    crowd[0] = Person("James");
    but Person2 is a twin
    crowd[1] = Person(crowd[0]);

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Quote Originally Posted by Dogplatter
    I'm working on a reference (or possibly a pointer) solution now, good thinking (no idea if it's going to work though).
    No. Don't use a pointer. Not unless you plan to change to a different referent inside your function. Otherwise, references are a much better choice... and the syntax to access them much simpler.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I don't get the problem. You can have only a single implementation of selfRefer(). Since you hard-code the instance it refers to, there is only need for this one instance to be known to selfRefer(). Thus, you can declare, or even define, this single instance above selfRefer() and should have no problem.

    What is it that you are doing where this fails?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    why cant extern be used?

    Code:
    <file: foo.h>
    class Foo
    {
    
    };
    
    extern Foo bar_array[10]; // note: nothing is defined here.
    
    < some .cpp file >
    #include "foo.h"
    
    Foo bar_array[10]; // now bar_array is defined.
    
    ... implementations of the methods of Foo go here, which can of course access the bar_array
    
    < some other .cpp file>
    #include "foo.h"
    
    int main()
    {
      bar_array[4] = Foo(); // valid, since bar_array was declared extern in foo.h 
      return 0;
    }

    also, when you put your code that way:
    Code:
    Class myClass {
    //never mind constructors and destructors, they aren't relevant here
    public:
    void SelfRefer();
    int Foo;
    
    };
    
    myClass Billy[2];
    
    void myClass::Selfrefer() {
    Billy[1].Foo=3;
    }
    you shouldn't have any problems then.
    Last edited by Raven Arkadon; 07-13-2006 at 08:51 PM.
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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