a class sharing the public struct of another class

V

Victor Hannak

I have a class like this:
<LarryClass.h>
class LarryClass {

public:
struct MoeLinkedListStruct {
LarryClass *Larry;
float MoeValue;
MoeLinkedListStruct *NextMoe;
};
LarryClass();
~LarryClass();
void Eval(float Value, MoeLinkedListStruct *&MoePtr);

private:
.....
}

I also have another class wherein I would like to be able to dynamically
create a new MoeLinkedListStruct like this:
<MoeClass.h>
#include "LarryClass.h"

<MoeClass.cpp>
MoeLinkedListStruct *MoePtr = new MoeLinkedListStruct;
LarryClass *Larry = new LarryClass;
Larry->Eval(0,MoePtr);

So the question is this: how do I make the MoeLinkedListStruct visible to
LarryClass? Since the struct is defined in the public area of LarryClass,
shouldn't the above code work? Am I just missing something small? I can't
really put the structure definition into a different file, because it has a
LarryClass variable in it, so LarryClass must already be defined. What else
could I do here? By the way, the intent here is for the Eval function in
LarryClass to be able to modify MoePtr to point to a different Moe, and then
pass that back up to the MoeClass.

Thanks, Vic
 
J

John Harrison

Victor Hannak said:
I have a class like this:
<LarryClass.h>
class LarryClass {

public:
struct MoeLinkedListStruct {
LarryClass *Larry;
float MoeValue;
MoeLinkedListStruct *NextMoe;
};
LarryClass();
~LarryClass();
void Eval(float Value, MoeLinkedListStruct *&MoePtr);

private:
....
}

I also have another class wherein I would like to be able to dynamically
create a new MoeLinkedListStruct like this:
<MoeClass.h>
#include "LarryClass.h"

<MoeClass.cpp>
MoeLinkedListStruct *MoePtr = new MoeLinkedListStruct;
LarryClass *Larry = new LarryClass;
Larry->Eval(0,MoePtr);

So the question is this: how do I make the MoeLinkedListStruct visible to
LarryClass? Since the struct is defined in the public area of LarryClass,
shouldn't the above code work? Am I just missing something small?

This should work.

LarryClass::MoeLinkedListStruct *MoePtr = new
LarryClass::MoeLinkedListStruct;
I can't
really put the structure definition into a different file, because it has a
LarryClass variable in it, so LarryClass must already be defined.

Not true, because MoeLinkedListStruct only has a pointer to LarryClass you
could use a forward declaration.

class LarryClass; // forward declaration

struct MoeLinkedListStruct {
LarryClass *Larry;
float MoeValue;
MoeLinkedListStruct *NextMoe;
};

This will compile without LarryClass being fully defined.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top