GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Oct-2008, 18:50
kuroi kuroi is offline
New Member
 
Join Date: Oct 2008
Posts: 5
kuroi is on a distinguished road

Classes referencing one another


Is there some way i can create an object refering to a class which will be declared only AFTER the instance of the object??

For example, imagine this situation:
CPP / C++ / C Code:
class x
{
      public : x()
      {
             /* code */
      }
};
class y 
{
      public : y()
      {
             x xxx = x(); //instance of an object of class x
      }
};
okay it works nice.

now imagine this:
CPP / C++ / C Code:
class x
{
      public : x()
      {
             y yyy = y(); //instance of an object of class y
      }
};
class y 
{
      public : y()
      {
             x xxx = x(); //instance of an object of class x
      }
};
the compiler returns error because the class y will be declared only after the object have been instanced.
it works well on c# but not on c++. is there some way to get this to work??
i know i can declare functions first and type the code after it is used, but i can't do that with classes, is there some way??
and, still, this is even more complicated because classes x and y will be in different headers (h. files). is there a way to make this work?? thanks.
  #2  
Old 08-Oct-2008, 19:38
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: classes referencing one another


Quote:
Originally Posted by kuroi
Is there some way i can create an object refering to a class which will be declared only AFTER the instance of the object??
Classes require knowledge of the size of each member encapsulated. This creates a problem that forward references alone cannot solve.

The solution is to use a pointer within at least one of the classes. eg.
CPP / C++ / C Code:
class foo;

class bar {
    foo *p;
};

class foo {
    bar b;
};

int
main() {
    foo f;
    bar b;

    return 0;
}
  #3  
Old 08-Oct-2008, 19:47
kuroi kuroi is offline
New Member
 
Join Date: Oct 2008
Posts: 5
kuroi is on a distinguished road

Re: classes referencing one another


thanks. now i've been able to create the objects, but still it doens't let me use any methods or proprietys or even the constructor of class y inside of class x (he says 'name_of_method' undeclared (first use this function)). is there some way around it??
  #4  
Old 08-Oct-2008, 19:56
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: classes referencing one another


Quote:
Originally Posted by kuroi
...it doens't let me use any methods or proprietys or even the constructor of class y inside of class x. is there some way around it??
Recall that the default access modifier with classes is private. Either relax the access modifiers used, or declare the classes to be friends of each other.
  #5  
Old 08-Oct-2008, 23:06
kuroi kuroi is offline
New Member
 
Join Date: Oct 2008
Posts: 5
kuroi is on a distinguished road

Re: classes referencing one another


yes, but everything is declared as public.
forgeting about the cross reference, i'm now trying to make this work.

content of x.h:
CPP / C++ / C Code:
class y;
          
class x
{
      public : int var;
     
      public : x()
      {
             y *yyy();             
             var = yyy->ret();
      }
};

content of y.h:
CPP / C++ / C Code:
class y 
{
      public : y()
      {

      }
      
      public : int ret()
      {
          return 5;
      }
};

content of the .cpp file:
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

#include <x.h>
#include <y.h>

int main()
{
    x obj = x();
    
    printf("%d", obj.var);
    
	getch();
	
	return 0;
}

what happens is when i try to compile, dev c++ tells me request for member `ret' in `yyy', which is of non-class type `y*()()' and points this line var = yyy->ret();. which i tried to rewrite many ways like:
var = yyy.ret();
var = *yyy->ret();
and etc but none works.

and still, i have verified that when i the line y *yyy(); is executed, the y() constructor is not called.

what should i do in this case?? thanks.
  #6  
Old 09-Oct-2008, 00:12
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: classes referencing one another


Quote:
Originally Posted by kuroi
..and still, i have verified that when i the line y *yyy(); is executed, the y() constructor is not called.
Correct. You aren't calling the constructor with that syntax. The syntax you are quoting creates the prototype for a function. If you want to allocate a new instance of a class at runtime, the required syntax is:
CPP / C++ / C Code:
y *p = new y;
...for the default constructor. If you want to call another constructor form which takes arguments, then use the following form:
CPP / C++ / C Code:
y *p = new y(/* argument0, argument1, ... */);
The following statements:
CPP / C++ / C Code:
y *yyy();             
var = yyy->ret();
...should generate a segmentation fault because a class instance has never been created. It appears that you need to study heapspace allocation with operator new(). One source covering this topic can be found at the following:

http://www.cplusplus.com/doc/tutorial/dynamic.html
  #7  
Old 09-Oct-2008, 00:54
kuroi kuroi is offline
New Member
 
Join Date: Oct 2008
Posts: 5
kuroi is on a distinguished road

Re: Classes referencing one another


thanks, that's a good link too, i'm still learning c and am doing it on my own.
but... still, this way:
CPP / C++ / C Code:
      public : x()
      {
             y *yyy = new y;
             var = yyy->ret();
      }
i get the message invalid use of undefined type `struct y' pointing to the two lines above.

of course it works well if i include y.h before x.h, but my intention is that one of the classes in y.h will also reference a class from x.h.
heheh i'm beginning to assume either it's impossible, or there's something i'm just not being able to figure out.

well, let me describe better what really i intend to do for maybe there may be another way around it. what happens is that i'm creating a kinda large library, so instead of using a single .h file, i'm splitting it in differents files. each one of the files correponds to a topic of the library.
now, what happens is that many classes of each file will work with classes of other files.
now i'll have a lot of trouble if i have to worry about which class is declared first, and still, i'll never get it to work, since many classes will need many other classes the crossings will never get right and still, probably, two classes will work with (reference) each other like the example with x and y classes from my top post.

thanks for the help till now, got any ideas??
  #8  
Old 09-Oct-2008, 01:33
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Classes referencing one another


Quote:
Originally Posted by kuroi
of course it works well if i include y.h before x.h, but my intention is that one of the classes in y.h will also reference a class from x.h.
Correct. The forward reference only establishes (delays) the fact that y represents a user-defined type (class). What the compiler is complaining about here is the call to member function ret(). It has no advanced information about it without seeing the class declaration found in the header file, so it calls this error out to the user.
Quote:
i'm beginning to assume either it's impossible...
Creating an instance of y within the constructor to x means that an x object will create an y which in turn will create another x which creates yet another y, etc. The result of this seemingly infinite chain of events is that the stack (which is of finite size...) will be exceeded which will cause the application to crash during execution.
Quote:
got any ideas??
Again, I don't know what this is all meant to model, but as it is, you are describing the composition of tightly coupled classes. Does this really describe the relationship, or is the relationship better defined as one class refining/extending the behavior of the other? "is-a" relationships are better characterized by inheritance rather than composition.
Last edited by ocicat : 09-Oct-2008 at 02:29.
  #9  
Old 09-Oct-2008, 02:45
kuroi kuroi is offline
New Member
 
Join Date: Oct 2008
Posts: 5
kuroi is on a distinguished road

Re: Classes referencing one another


Quote:
Originally Posted by ocicat
Creating an instance of y within the constructor to x means that a y will create an x which will then create another y which creates yet another... The result of this chain is that the stack will be exceeded which will crash execution.
well that was only an example, i don't really intend to create it in the constructor, i just put there so that the code i'd post here would be smaller. i thought about crashing, in fact i knew it would crash, for i tested something like that (the crossing reference on both constructors) on c# and it did crash (but it worked well when i trasnfered the object references to another method... but those are two different languages, it is sad i can't do that easily on c++), but that doesn't matter to me, i was just hoping it to compile, so that i would know how to implementate it in my library.


Quote:
Originally Posted by ocicat
Does this really describe the relationship, or is the relationship better defined as one class refining/extending the behavior of the other?
no, it's not that the case. but well... maybe i got kinda confused among the object orientation concepts, i'll think about this and maybe was trying to do the wrong way... if get to a conclusion, i post here again...

but... i was thinking about some stuff i've seen... for example, on .net i have the class frame (which is a box to insert objects inside) and i have the class panel (which is a different box but also used to insert objects inside). and then, i can insert a panel inside of a frame and a frame inside of a panel.
so i was thinking about insert an x object inside of an y and an y inside of a x. and i could make this exactly the way i posted above on .net.
another example is that a control inside the form (on .net or visual basic 6) has the property called Container... which returns the form he is inside.
but well.. i'll rethink this, and when i get to a conclusion, i'll post here, thanks.
  #10  
Old 09-Oct-2008, 03:10
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Classes referencing one another


Quote:
Originally Posted by kuroi
...for i tested something like that (the crossing reference on both constructors) on c# and it did crash (but it worked well when i trasnfered the object references to another method...
Correct. The same will be true in C++; just realize that exercising the tight coupling within the constructor is problematic.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes initiation kdsXchris C++ Forum 3 05-Jun-2006 04:07
can't figure this out (classes cross referencing) ehudros C++ Forum 4 12-May-2006 10:29
Re: Derived FLTK Classes cable_guy_67 FLTK Forum 0 26-Jun-2005 21:07
Assistance with classes... Bravebird C++ Forum 7 27-Apr-2005 14:17
Fairly simple classes help please sammacs C++ Forum 0 30-Nov-2004 10:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:36.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.