May 05, 2024, 07:39:58 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Unknown type

Started by Protected, May 22, 2006, 03:11:46 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Protected

Imagine I want to create two classes - A and B. Class A will contain a number of instances of class B in its fields/class variables. So when I declare class A, I must have some variables of the type 'B' in its declaration. On the other hand, I'd like each instance of B to keep a reference to its parent instance, so when I declare B I must have a variable of the type 'A'. Now... How do I make this work? :P No matter which class I declare first, I'll always get an 'Unknown type' error when compiling because the other class was not declared yet!

Ionic Wind Support Team

The chicken before the egg ehh ;)

I normally handle situations like this with a void pointer.

class B
{
void *m_pParent;
}

class A
{
declare A;
B m_1;
B m_2;
}

A::A()
{
m_1.m_pParent = this;
m_2.m_pParent = this;
}

Then when your in a method of class B and want to access the parent use a typecast

*(A)m_pParent.DoSomething();

C uses forward references, something we don't have as of yet.  The forward reference tells the compiler that somewhere in the source an object will be defined.  It allows you to define a pointer to that object, but not much else.
Ionic Wind Support Team

Protected

I see. Isn't there a more practical way to access the parent instance of the instance of a class?

Ionic Wind Support Team

Only you are making it a "parent".  Unless your talking about deriving from a base class in which case that is a different subject.
Ionic Wind Support Team

Mike Stefanik

Create the inner class (the class that is being contained by the outer class) dynamically and use a pointer. For example:


class CInner
{
ÂÃ,  ÂÃ, declare CInner();
ÂÃ,  ÂÃ, declare _CInner();
ÂÃ,  ÂÃ, declare InnerMethod();

ÂÃ,  ÂÃ, void *m_pOuter;
}

class COuter
{
ÂÃ,  ÂÃ, declare COuter();
ÂÃ,  ÂÃ, declare _COuter();
ÂÃ,  ÂÃ, declare OuterMethod();

ÂÃ,  ÂÃ, CInner *m_pInner;
}

CInner::CInner()
{
ÂÃ,  ÂÃ, m_pOuter = null;
ÂÃ,  ÂÃ, return;
}

CInner::_CInner()
{
ÂÃ,  ÂÃ, m_pOuter = null;
ÂÃ,  ÂÃ, return;
}

CInner::InnerMethod()
{
ÂÃ,  ÂÃ, writeln("CInner::InnerMethod\n");

ÂÃ,  ÂÃ, if (m_pOuter != null)
ÂÃ,  ÂÃ, {
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, COuter *pOuter = m_pOuter;
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, pOuter->OuterMethod();
ÂÃ,  ÂÃ, }
ÂÃ,  ÂÃ, 
ÂÃ,  ÂÃ, return;
}

COuter::COuter()
{
ÂÃ,  ÂÃ, m_pInner = new(CInner, 1);

ÂÃ,  ÂÃ, if (m_pInner != null)
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, m_pInner->m_pOuter = this;
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, 
ÂÃ,  ÂÃ, return;
}

COuter::_COuter()
{
ÂÃ,  ÂÃ, if (m_pInner != null)
ÂÃ,  ÂÃ, {
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, delete m_pInner;
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, m_pInner = null;
ÂÃ,  ÂÃ, }

ÂÃ,  ÂÃ, return;
}

COuter::OuterMethod()
{
ÂÃ,  ÂÃ, writeln("COuter::OuterMethod\n");
ÂÃ,  ÂÃ, return;
}

global sub main()
{
ÂÃ,  ÂÃ, COuter outerClass;

ÂÃ,  ÂÃ, if (outerClass.m_pInner != null)
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, outerClass.m_pInner->InnerMethod();
ÂÃ,  ÂÃ, 
ÂÃ,  ÂÃ, while (GetKey() == "");
ÂÃ,  ÂÃ, return 0;
}

Mike Stefanik
www.catalyst.com
Catalyst Development Corporation

Mike Stefanik

Quote from: Protected on May 22, 2006, 03:22:15 PM
I see. Isn't there a more practical way to access the parent instance of the instance of a class?

Your terminology is a bit off, and so it's not clear what exactly you're talking about. Are you talking about inner and outer classes (i.e.: one class that contains a reference to another class), or derived classes (a class derived from a base class)?

In the former case, the method that Paul and I described is how you'd do it. In the case of derived class, then the derived class already has access to all of the methods and member variables of the base class, and there's no instance of that base class to get a pointer to. The derived class is a kind of the base class.
Mike Stefanik
www.catalyst.com
Catalyst Development Corporation

Protected

I know, so of course I wasn't talking about it. Thanks.

Parker

Forward referencing brings in all kinds of problems, for example class A has a B, which has a C, which inherits from A. All those kinds of things would need to be checked by the compiler when that's brought in.

J B Wood (Zumwalt)

"has-a" relationships
"is-a" relationship
"kind-of" relationship

All fun things, the real question is, the cart 'has-a' wheel, but 'is-a' wagon, witch is a 'kind-of' transportation :)
Pointers.. its a wonder why programming survived this long, this points at that which points at something else.. is it me or is programming nothing more than digitial accusations?