Thread: Classes storing instances of own class.

  1. #1
    Registered User
    Join Date
    Aug 2006

    Classes storing instances of own class.

    Could anyone tell me why the following code fragment is not valid. I get an Undefined Structure error

    class MyClass
              MyClass instanceOfClass;
    The class has to store a reference to another instance of the class. Actually reference is a bad term, technically a copy, since the original version of that instance may be deleted/moved by another part of the program.

    Im assuming that the problem is something to do with c++ wanting to know the size of everything at compile time. But then how do I get around it? Like I said, I cant use a pointer, because the instance of MyClass it points to may be either deleted or moved.

  2. #2
    Registered User
    Join Date
    Aug 2006

    I feel its incredibly messy, but Im currently using a vector<MyClass> to store my instance. While it looks and most likely is wasteful, not only does it work, but it also allows me to easily define a != NULL operator, where MyClass != null if the vector size is greater than 0.

    On which note, my translation from java to c++ is now essentially complete, with the exception of clearing it up and making it a little more prefessional. So thanks for all the help people have given merecently

  3. #3
    Registered User
    Join Date
    Mar 2006
    Well, instanceOfClass would have its own instanceOfClass, which would have its own instanceOfClass, and so on. It would run into an infinite loop. Since a vector may or may not hold a class instance, it (like a pointer or some other container object) provides a mechanism with which to terminate the hierachy at some point.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /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;}

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    In other words, an object containing an instance of its own class is of infinite size and therefore illegal.
    All the buzzt!

    "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

  5. #5
    Registered User
    Join Date
    May 2003
    And you can get deep copy semantics with a pointer, too; you just need to manually allocate/deallocate, or use a smart pointer. Pointers are exactly why Java permits this sort of thing.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class with pointers to other classes as member data.
    By Sclorch in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2009, 05:59 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. organizing classes in a game
    By MathFan in forum Game Programming
    Replies: 2
    Last Post: 04-28-2005, 09:23 AM
  4. Class with members as classes
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 06:58 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM