C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old November 6th, 2002, 05:41 PM
gamgee gamgee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Ireland
Posts: 26 gamgee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.....

Reply With Quote
  #2  
Old November 6th, 2002, 06:43 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
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.

Reply With Quote
  #3  
Old November 7th, 2002, 06:48 AM
Robo Robo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Posts: 29 Robo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old November 7th, 2002, 01:18 PM
isvara isvara is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: UK
Posts: 2 isvara User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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_;
};

Reply With Quote
  #5  
Old November 7th, 2002, 01:29 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
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.

Reply With Quote
  #6  
Old November 7th, 2002, 06:54 PM
Robo Robo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Posts: 29 Robo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yip, in Java, objects are called by references, similar to pointers in C/C++

Reply With Quote
  #7  
Old November 13th, 2002, 06:51 AM
pschmerg pschmerg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38 pschmerg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m
Reputation Power: 6
Send a message via AIM to pschmerg
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:
};

Reply With Quote
  #8  
Old November 13th, 2002, 11:43 AM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
Heh, everyone's saying the same thing, they just all have different names for it...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Using an object within itself


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway