Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 31
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
All today's birthdays', congrats!
Mathias (41)
slash128v6 (52)


Next birthdays
01/31 Mathias (41)
01/31 slash128v6 (52)
02/01 Barry (70)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

C++ Classes cross referencing pointers (communication between forms)

Move Thread LAN_403
TheMerovingian
Fri Jan 30 2009, 10:29PM Print
TheMerovingian 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

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.