470,855 Members | 1,157 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

Cyrcle reference problem...

Hello

I have struct, and class that handle array of previous mentioned structs.
How to declare struct member, that will point to class that it belongs?
struct S
{
//HOW TO ADD MEMBER THAT WILL POINT TO CLASS C ???
}

class C
{
S** items;
}

Thanks
Milan
Jul 22 '06 #1
3 840
MilanB wrote:
Hello

I have struct, and class that handle array of previous mentioned
structs. How to declare struct member, that will point to class that
it belongs?
struct S
{
//HOW TO ADD MEMBER THAT WILL POINT TO CLASS C ???
}

class C
{
S** items;
}
You need to use a "forward declaration":

class C;

struct S
{
C* m_c; // you can use C* or C&, but not just C
};

class C
{
S** items;
};

-cd
Jul 22 '06 #2
Thanks Daniel.
You solved my problem succesfully.

I would like to you ask about situation that I want to have memeber in
struct S, that point to function in class C (not whole class) does it changes
situation? (All other are the same)

struct S
{
//POINTER TO CLASS C MEMBER FUNCTION: IncrementSelectedCount()
};

class C
{
S** items;
private: int selectedCount;
private: void incrementSelectedCount(void)
{
selectedCount++;
}
};
"Carl Daniel [VC++ MVP]" wrote:
MilanB wrote:
Hello

I have struct, and class that handle array of previous mentioned
structs. How to declare struct member, that will point to class that
it belongs?
struct S
{
//HOW TO ADD MEMBER THAT WILL POINT TO CLASS C ???
}

class C
{
S** items;
}

You need to use a "forward declaration":

class C;

struct S
{
C* m_c; // you can use C* or C&, but not just C
};

class C
{
S** items;
};

-cd
Jul 23 '06 #3
OK. I Found it.
For other who maybe need same help here is solution:

class MyClass; //Forward declaration

//========================================

struct MyStruct
{
private: MyClass *myClass;
private: void (MyClass::*ptrToMemFunction)();
public: void CallFunction()
{
//Calling member function from referenced class
(myClass->*ptrToMemFunction)();
}

//On costructor pass Class and Function Member pointer
public :MyStruct(MyClass *parMyClass, void (MyClass::*parPtrToMemFunction)())
{
MyStruct::myClass = parMyClass;
MyStruct::ptrToMemFunction = parPtrToMemFunction;
}
//========================================
class MyClass
{
private: void memberFunction(void)
{
//do something
}
//Pass references..
private: void addNew(...) //some function...
{
..
..
..
void (MyClass::*ptrToMemberFunction)() = memberFunction;
MyStruct myStruct = new MyStruct(this, ptrToMemberFunction);
//Now we have struct that have valid pointer to MyClass::memeberFunction
..
..
}

};

Regards
Milan
Jul 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pkpatel | last post: by
7 posts views Thread by Nick Zdunic | last post: by
5 posts views Thread by Michael Russell | last post: by
27 posts views Thread by David W | last post: by
5 posts views Thread by Nightfall | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.