Archived

This topic is now archived and is closed to further replies.

Drakon

Accessing classes before they are "made"

Recommended Posts

I need to make references to a class B within class A, but class B is made after class A so it has no idea what class B is when I want it.

class A
{
public:
      Object   ObjRef;
};

class Object
{
public:
      int Blah;
      Object   IWish;
}
Also, how can I make a reference to its own class, like the IWish thing there? That causes this to happen: c:\vc++ projects\objhandler\classes.h(24) : error C2460: ''IWish'' : uses ''Object'', which is being defined

Share this post


Link to post
Share on other sites
1. You cant use Object before it''s been declared, so switch the order on the two classes.
2. If you want to access it like this you have to declare a pointer to it. It would be impossible to declare the same object in the same class since it would have to create an "Object" for each "Object" created (will recurse in all infinity).

class Object{
public:
int Blah;
Object * IWish;
};
class A
{
public:
Object ObjRef;
};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I should have complicated it... This is a more accurate description of what I have. Each class references each other.

class A
{
public:
Object ObjRef;
};

class Object
{
public:
A ARef;
};

Share this post


Link to post
Share on other sites
I should have complicated it... This is a more accurate description of what I have. Each class references each other.


class A
{
public:
Object ObjRef;
};

class Object
{
public:
A ARef;
};


[edited by - Drakon on April 4, 2004 10:58:17 AM]

Share this post


Link to post
Share on other sites
If you want them both in the same file, just declare the class you need, and define it later:


class Object; // now class A and class Object can both use Object

class A
{
public:
Object ObjRef;
};

class Object
{
public:
int Blah;
Object IWish;
};

Share this post


Link to post
Share on other sites
it says:

c:\vc++ projects\objhandler\classes.h(27) : error C2079: ''RoomContainer'' uses undefined class ''Room''

(not using those same classes in my example... I use Object and Room)

Share this post


Link to post
Share on other sites

#pragma once
#include <cstring>
#include <string>
#include <sstream>
using namespace std;

class Room;

class Object
{
public:
int ONum;
int Hierarchy;
vector<int> Item;
vector<int> ItemHierarchy;
vector<Object> OItem;
int Container;
int ContainerHierarchy;
Object *ObjectContainer;
Room RoomContainer;
bool ContainerIsRoom;
};

class Room
{
public:
int RNum;
vector<int> Item;
vector<Object> OItem;
};

Share this post


Link to post
Share on other sites
I''m not sure why that doesn''t work, but I tried just flipping it around, and it works:


class Object;

class Room
{
public:
int RNum;
vector<int> Item;
vector<Object> OItem;
};

class Object
{
public:
int ONum;
int Hierarchy;
vector<int> Item;
vector<int> ItemHierarchy;
vector<Object> OItem;
int Container;
int ContainerHierarchy;
Object *ObjectContainer;
Room RoomContainer;
bool ContainerIsRoom;
};


However, you may want to rethink your design anyway: an object contains a room? And a room contains a list of objects, each of which contains a room?

Share this post


Link to post
Share on other sites
You can''t have a recursive containment, as that would require infinite storage (think about it).

What you can have is recursive references, using a pointer or a C++ reference.

In this case you can partially declare a class:


class B; // Tells compiler "Class B will exist"

class A {
private:
B *bptr;
public:
void doSomething();
};

class B {
private:
A *aptr;
// etc
}

Share this post


Link to post
Share on other sites
This circular dependency is a bad thing™, either you should think up an interface that Room can implement (that both Room and Object knows about) or you might want to go crazy and look up the observer-pattern.

Share this post


Link to post
Share on other sites