Accessing private mambers from different class.

For example i have two files:
1: "Form1.h"
2: "Form2.h"

How to access Form2 class private members from Form1 class?
And vice versa.

Form1.h code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once

namespace MyApp {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = gcnew System::ComponentModel::Container();
			this->Size = System::Drawing::Size(300,300);
			this->Text = L"Form1";
			this->Padding = System::Windows::Forms::Padding(0);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		}
#pragma endregion
	};
}



Form2.h code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once

namespace MyApp {

using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form2
	/// </summary>
	public ref class Form2 : public System::Windows::Forms::Form
	{
	public:
		Form2(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form2()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = gcnew System::ComponentModel::Container();
			this->Size = System::Drawing::Size(300,300);
			this->Text = L"Form2";
			this->Padding = System::Windows::Forms::Padding(0);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		}
#pragma endregion
	};
}


Im using Visual C++ 2010
The traditional way to do this is by making the classes 'friends' of one and other: http://www.cplusplus.com/doc/tutorial/inheritance/
An even better way is to simply not do this.

Friendship should only be used on classes which are very tightly bound -- like different components of a larger structure. A typical example is a container class and an iterator class for that container... they're obviously codependent so it makes sense for them to be friends.


If you have two classes that are not tightly bound... then making them friends defeats the entire point of making your data private in the first place. At which point you might as well just make the data public because you're already violating encapsulation.


My advice: if you need these classes to access each other's private data, you likely have a design flaw. Rethink your design and class structure.
Last edited on
when i try to make From1 and Form2 friends i get error:
error C3809: 'MyApp::Form1': a managed type cannot have any friend functions/classes/interfaces

I could just make them public but in the future i might want to have Form3.h and Form4.h and if i would make Form1 class and Form2 class members public my Form3 and Form4 would have access and i dont want that.

Is there any way around this?
I looked a lot about this in google and looks like there is no decent way to do this but if you need information on " C++ / CLI Inheritance " :
http://www.functionx.com/cppcli/classes2/Lesson07b.htm

And i would suggest to read about " Multiple Inheritance in C++ " :
http://www.cprogramming.com/tutorial/multiple_inheritance.html
Topic archived. No new replies allowed.