The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Using an object within itself
Discuss Using an object within itself in the C Programming forum on Dev Shed. Using an object within itself C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 6th, 2002, 05:41 PM
|
|
Junior Member
|
|
Join Date: Oct 2002
Location: Ireland
Posts: 26
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Using an object within itself
Is it possible to use and object within itself?
For example I have a class called state. Can a state object also hold other states?
Im sure the n queens problem comes in here somewhere.....
|

November 6th, 2002, 06:43 PM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
|
No, I don't think so. You'd create an overflow, as you're likely referring to about the queens. The first class has memory allocated for its members, of which is another instance of the class, which has members, of which is another instance of the class...never ending loop.
You could use work arounds though, where objects are declared on the heap if some condition is met. Each class instance stores pointers to said objects, which would be null if no object was created. The condition could be how deeply nested your classes are, or whatever you need.
|

November 7th, 2002, 06:48 AM
|
|
Registered User
|
|
Join Date: Nov 2001
Posts: 29
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I can't speak for C++, but you can definitely do that in Java, so I'll be surprise if that's not possible in C++. One of the elementry data structure is the Link List, you create a Link object, and inside that, it can hold other Link objects.
Actually, in a Link object, it holds references to other Link objects.
Code:
class Link {
private Link nextLink;
private Link prevLink;
private Object dataItem;
Link() {
}
...
}
Last edited by Robo : November 7th, 2002 at 06:53 AM.
|

November 7th, 2002, 01:18 PM
|
|
Junior Member
|
|
Join Date: Nov 2002
Location: UK
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Yes, it's possible to use objects within other objects of the same class, but they must be aggregated rather than contained. For example:
class State
{
State *otherState_;
};
|

November 7th, 2002, 01:29 PM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
|
Exactly. I suspect that is what happens in Robo's java example as well, but I've never used the language. Robo's example seems like an implementation of a link list, where each link holds pointers to the previous and next link, if any. But no link actually contains other links.
|

November 7th, 2002, 06:54 PM
|
|
Registered User
|
|
Join Date: Nov 2001
Posts: 29
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Yip, in Java, objects are called by references, similar to pointers in C/C++
|

November 13th, 2002, 06:51 AM
|
|
Contributing User
|
|
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38
Time spent in forums: 2 h 29 m
Reputation Power: 11
|
|
|
you can certainly do this in c++. you just need to use a predeclaration for the class. i can't quite remember but the class should look something like this
class state; //pre declaration must be used, it's like an IOU
class state
{
private:
state* pState;
public:
};
|

November 13th, 2002, 11:43 AM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
|
Heh, everyone's saying the same thing, they just all have different names for it...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|