Two classes
-
I've got a problem which I can't solve. I want to have 2 classes and both of them to have a pointer to each other...
public __gc class a
{
public:
a::a()
{
m_b->bb();
}int aa() { return 1; }
private:
b* m_b;
};public __gc class b
{
public:
b::b()
{
m_a->aa();
}
int bb()
{
return 1;
}
private:
a* m_a;
};The code isn't able to compile. One solution is to use Object* instead of b* in the class a but I'll have to move my declaration to a cpp file and cast the pointer every time I want to use it. Mayby another solution? :confused: :confused: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c
-
I've got a problem which I can't solve. I want to have 2 classes and both of them to have a pointer to each other...
public __gc class a
{
public:
a::a()
{
m_b->bb();
}int aa() { return 1; }
private:
b* m_b;
};public __gc class b
{
public:
b::b()
{
m_a->aa();
}
int bb()
{
return 1;
}
private:
a* m_a;
};The code isn't able to compile. One solution is to use Object* instead of b* in the class a but I'll have to move my declaration to a cpp file and cast the pointer every time I want to use it. Mayby another solution? :confused: :confused: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c
-
I've got a problem which I can't solve. I want to have 2 classes and both of them to have a pointer to each other...
public __gc class a
{
public:
a::a()
{
m_b->bb();
}int aa() { return 1; }
private:
b* m_b;
};public __gc class b
{
public:
b::b()
{
m_a->aa();
}
int bb()
{
return 1;
}
private:
a* m_a;
};The code isn't able to compile. One solution is to use Object* instead of b* in the class a but I'll have to move my declaration to a cpp file and cast the pointer every time I want to use it. Mayby another solution? :confused: :confused: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c
Use
__single_inheritance
keyword! This is an example://Declarations:
public __gc class __single_inheritance b;
public __gc class a;
//Definitions:
public __gc class a
{
public:
a::a(){}
public:
b* m_b;
System::String* stringInClassA;
};public __gc class b
{
public:
b::b(){}
public:
a* m_a;
System::String* stringInClassB;
};// This is the entry point for this application
int _tmain(void)
{
a* A = new a();
b* B = new b();
A->stringInClassA = new String("This is string in class A");
B->stringInClassB = new String("This is string in class B");
A->m_b = B;
B->m_a = A;
Console::WriteLine(A->m_b->GetType()->ToString());
Console::WriteLine(B->m_a->GetType()->ToString());
Console::WriteLine("");
Console::WriteLine("from class A:");
Console::WriteLine(A->m_b->stringInClassB);
Console::WriteLine("");
Console::WriteLine("from class B:");
Console::WriteLine(B->m_a->stringInClassA);
int i = Console::Read ();return 0;
}The matrix has you :suss: MP Maciej Pirog
-
Use
__single_inheritance
keyword! This is an example://Declarations:
public __gc class __single_inheritance b;
public __gc class a;
//Definitions:
public __gc class a
{
public:
a::a(){}
public:
b* m_b;
System::String* stringInClassA;
};public __gc class b
{
public:
b::b(){}
public:
a* m_a;
System::String* stringInClassB;
};// This is the entry point for this application
int _tmain(void)
{
a* A = new a();
b* B = new b();
A->stringInClassA = new String("This is string in class A");
B->stringInClassB = new String("This is string in class B");
A->m_b = B;
B->m_a = A;
Console::WriteLine(A->m_b->GetType()->ToString());
Console::WriteLine(B->m_a->GetType()->ToString());
Console::WriteLine("");
Console::WriteLine("from class A:");
Console::WriteLine(A->m_b->stringInClassB);
Console::WriteLine("");
Console::WriteLine("from class B:");
Console::WriteLine(B->m_a->stringInClassA);
int i = Console::Read ();return 0;
}The matrix has you :suss: MP Maciej Pirog
Ok, but try to call a bb function on the m_b instance in the a class contructor. It won't compile. Any other tips? :cool: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c
-
It won't compile too because class b has a field m_a of class a. 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c
-
Ok, but try to call a bb function on the m_b instance in the a class contructor. It won't compile. Any other tips? :cool: 43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c
Yoe'r right :-O But I've got an idea! Put: 1st classes declarations 2nd classes definitions and functions declarations 3rd functions definitions Example:
// Classes declarations:
public __gc class __single_inheritance b;
public __gc class __single_inheritance a;
// Classes definitions and functions *declarations*:
public __gc class a
{
public:
void AskClassBToWrite();
void WriteA();
b* m_b;
};public __gc class b
{
public:
void AskClassAToWrite();
void WriteB();
a* m_a;
};
// functions *definitions*
void a::WriteA()
{
Console::Write("WriteA function was called");
}void a::AskClassBToWrite()
{
a::m_b->WriteB();
}void b::WriteB()
{
Console::Write("WriteB function was called");
}void b::AskClassAToWrite()
{
b::m_a->WriteA();
}// This is the entry point for this application
int _tmain(void)
{
a* A = new a();
b* B = new b();A->m\_b = B; B->m\_a = A; A->AskClassBToWrite(); B->AskClassAToWrite(); Console::Read(); return 0;
}
This will work! :) MP Maciej Pirog
-
Yoe'r right :-O But I've got an idea! Put: 1st classes declarations 2nd classes definitions and functions declarations 3rd functions definitions Example:
// Classes declarations:
public __gc class __single_inheritance b;
public __gc class __single_inheritance a;
// Classes definitions and functions *declarations*:
public __gc class a
{
public:
void AskClassBToWrite();
void WriteA();
b* m_b;
};public __gc class b
{
public:
void AskClassAToWrite();
void WriteB();
a* m_a;
};
// functions *definitions*
void a::WriteA()
{
Console::Write("WriteA function was called");
}void a::AskClassBToWrite()
{
a::m_b->WriteB();
}void b::WriteB()
{
Console::Write("WriteB function was called");
}void b::AskClassAToWrite()
{
b::m_a->WriteA();
}// This is the entry point for this application
int _tmain(void)
{
a* A = new a();
b* B = new b();A->m\_b = B; B->m\_a = A; A->AskClassBToWrite(); B->AskClassAToWrite(); Console::Read(); return 0;
}
This will work! :) MP Maciej Pirog
Thanks man. It's working. :) :) :)
43 68 65 65 72 73 2c 4d 69 63 68 61 65 6c