Cyrcle reference problem...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
MilanB said:
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
 
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++;
}
};
 
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
 
Back
Top