Sunday, August 2, 2009

C/C++ - nested objects/structs question?

I have a great idea of how to implement an algebra solving/simplfying program, but I need it to have a structure within itself. Here's what I mean:





struct thing {


thing *t1;


thing *t2;


int other;


};





As you can see, the struct uses itself, and therefore cannot define 'thing' until it is finished, making a sort of catch 22. Is there a way to do something like this? All it needs to do is store two pointers to other instances of the same data type and one integer. If it is possible with a class, that would work too.





The code can be completely different, as long as it does what I described or gives an alternate method.

C/C++ - nested objects/structs question?
Well my C is a bit rusty but I know it is officially impossible to declare a structure containing an instance of itself....talk about endless loop! so doubt C++ would allow it either as physically impossible.





Couldn't you make a small array to hold the pointers and int? you could use an ordinary do-while to loop through?
Reply:Pointers to similar structures or classes is a common concept in data structures. This is how things such as linked lists or trees. They have a structure with a pointer the same structure for the next one in the list or the children in the tree. Look up linked list in wikipedia to see what I mean.





Anyways, here is some code demonstrating what you want to do. This is in C, but you can do similar things with classes in C++. I also played with the variables a bit to demonstrate different ways of accessing the data.





#include %26lt;stdio.h%26gt;





//get the compiler to recognize Thing as its own data type





typedef struct Thing Thing;





//define the struct





struct Thing {


Thing *first;


Thing *second;


int val;


};





int main()


{


Thing o1;


Thing o2;


Thing o3;





Thing *p4;


Thing *p5;


Thing *p6;





//dynamicall created Things


p4 = (Thing*)malloc(sizeof(Thing));


p5 = (Thing*)malloc(sizeof(Thing));


p6 = (Thing*)malloc(sizeof(Thing));





o1.first = %26amp;o2;


o1.second = %26amp;o3;





o1.val = 1;


o2.val = 2;


o1.second-%26gt;val = 3; // does same as o3.val = 3





p4-%26gt;first = p5;


p4-%26gt;second = p6;





p4-%26gt;val = 4;


p5-%26gt;val = 5;


p4-%26gt;second-%26gt;val = 6; // does same as p6-%26gt;val = 6;





printf("%d\n", o1.val);


printf("%d\n", o1.first-%26gt;val);//same as printing o2.val


printf("%d\n", o3.val);





printf("%d\n", p4-%26gt;val);


printf("%d\n", p4-%26gt;first-%26gt;val); //same as printing p5-%26gt;val


printf("%d\n", p6-%26gt;val);





//free dynamically created objects


free(p4);


free(p5);


free(p6);





return 0;


}
Reply:Use forward class/struct declaration, for example:





struct thing;





struct thing {


thing *t1;


thing *t2;


int other;


};
Reply:first thought : try the 'this' pointer in a class .. might work as such





#include %26lt;iostream%26gt;


using namespace std;


class many


{


public:


int integer1;


many *ptr;





many()


{


integer1 = 0;


ptr = this;


}


};





void main()


{


many o;


cout%26lt;%26lt;o.integer1%26lt;%26lt;endl;





cout%26lt;%26lt;o.ptr-%26gt;integer1%26lt;%26lt;endl;


}


No comments:

Post a Comment