Archived

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

struct in class

This topic is 5588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I remeber correctly there was a way to declare and use a struct in a class, where you fill the struct in another part of the program: // some.h class sample{ public: struct HERE; }; // main.cpp #include some.h ... struct HERE{ int x; }; ... This was possible right? Can anyone give me an example please?

Share this post


Link to post
Share on other sites
Try this:


  

//some.h


struct Struct {

int member1;
int member2;

};

class Class {

private:

int member1;
int member2;

public:

Struct classStruct;

//rest of class

};



  
//some.cpp


int main() {

int variable;
Class myclass

//...later in the function


myclass.classStruct.member1 = variable;

//...rest of function


}


Hope that helps.

John.

Share this post


Link to post
Share on other sites
You can do it if the member is a pointer or a reference:

class AClass
{
struct AStruct* s;

};


// somewhere else

struct AStruct
{

};

Of course any code which actually uses s needs to have the declaration of AStruct included.

[edited by - JuNC on August 27, 2002 4:46:53 PM]

Share this post


Link to post
Share on other sites