By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

a class sharing the public struct of another class

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Victor Hannak" <victor.hannak@no****@rochester.rr.com> wrote in message
news:7J*******************@twister.nyroc.rr.com...
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
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.