C++ Classes cross referencing pointers (communication between forms)
|
|
TheMerovingian
|
Fri Jan 30 2009, 10:29PM
|
|
|
Registered Member #14
Joined: Thu Feb 02 2006, 01:04PM
Location: Prato/italy
Posts: 383
|
I need to communicate between two forms keeping both pointers in scope. After the declaration i create them:
Form1 ^myform1 = gcnew Form1();
Form2 ^myform2 = gcnew Form2();
myform1->SetForm2Pointer(myform2);
myform2->SetForm1Pointer(myform1);
Application::Run(myform1); And then i store the pointers using two public accessor functions
I define the prototype of the second class (Form2) before the acqual declaration of the Form1 class.
From Form2 i can access the the public properties of Form1 using the pointer
private:
Form1 ^pForm1;
Form2 ^pForm2; <- this is legal because the class prototype is declared this is a member function of Form2 accessing a public Form1 variable
void ChangeVariable()
{
this->GetForm1Pointer()->accessvariable = 123;
} But cannot do the converse:
void ChangeVariable() // public member of Form1
{
this->pForm2->NumTry = 342;
} Because the Form2 class isn't defined, so the members aren't accessible
it gives me an C2027 error
Is there a way to make a cross reference or have I to use one way references?
EDIT: i'm idiot.... i solved simply placing the member function prototype in the class and then defining the function after the classes definitions...
now it compiles
Also tried to define a class Forms inheriting from System::Windows::Forms:Form placing the common Pointer exchange functions (GetForm1Pointer etc) to avoid to define them for each class
public ref class Forms : public System::Windows::Forms::Form
{
private:
Form1 ^pForm1;
Form2 ^pForm2;
public:
int accessvariable;
void SetForm1Pointer(Form1 ^ptr)
{
this->pForm1 = ptr;
}
Form1 ^GetForm1Pointer()
{
return this->pForm1;
}
void SetForm2Pointer(Form2 ^ptr)
{
this->pForm2 = ptr;
}
Form2 ^GetForm2Pointer()
{
return this->pForm2;
}
};
public ref class Form1 : public Forms
{
public: So each class inherits Pointer management functions and it seems to work
so finally this is the procedure to Inter-Connect the Forms by OO
Define the Class prototypes
Create a Super-Class with pointer management functions (public) and pointers (private)
Each form must inherit from the super-class
define the prototypes of the member functions and then the member functions using the pointers
When spawning the Forms, remember to set the correct pointers
|
Back to top
|
|
Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob
|
|
Powered by e107 Forum System
|