I
Immortal_Nephi
Sometimes, unsigned char array is located in the file scope. You
define A Object and B Object. A Object and B Object need to share
unsigned char array. They are very different object. They are like
two chips.
A Object modifies data in the unsigned char array. Then, B Object is
in turn to modify data in the unsigned char array. Static data member
is not the answer. Unsigned char array in the file scope is a bad
idea because other functions or classes might modify unsigned char
array accidently. The only way is to guard it inside main() function.
You need to define two pointers. Two pointers are used to share
unsigned char array. You have to be careful when you write your code
in the correct sequence.
Sometimes, you may want to copy data member from A Object to B
Object. Friend class is the answer. Please say if Update() function
or Update2() function is better. Update2() function may have
additional overhead because "this pointer" reads memory address
indirection before indirection memory address is read again to another
indirection memory address and then accesses data member. Update()
function uses reference to A Object.
Compare Run() function in both A Object and B Object.
Place NULL in unsigned char array in both ~A() function and ~B()
function are unnecessary.
Please state your opinion what you think my good design C++ code.
Here is an example of my code below.
class B;
class A
{
public:
A( unsigned char *RAM )
{
this->RAM = RAM;
this->a = 'A';
this->b = 'B';
this->c = 'C';
}
~A() { this->RAM = 0; }
void Set_B_Ptr(B &b)
{
this->B_Ptr = &b;
}
void Run()
{
this->RAM[0] = this->a;
this->RAM[1] = this->b;
this->RAM[2] = this->c;
}
private:
friend class B;
unsigned char* RAM;
unsigned char a;
unsigned char b;
unsigned char c;
B *B_Ptr;
};
class B
{
public:
B( unsigned char *RAM )
{
this->RAM = RAM;
this->d = 'D';
this->e = 'E';
this->f = 'F';
}
~B() { this->RAM = 0; }
void Set_A_Ptr(A &a)
{
this->A_Ptr = &a;
}
void Run()
{
this->RAM[3] = this->d;
this->RAM[4] = this->e;
this->RAM[5] = this->f;
}
void Update(A &a)
{
this->a = a.a;
this->b = a.b;
this->c = a.c;
}
void Update2()
{
this->a = this->A_Ptr->a;
this->b = this->A_Ptr->b;
this->c = this->A_Ptr->c;
}
private:
friend class A;
unsigned char* RAM;
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
unsigned char e;
unsigned char f;
A *A_Ptr;
};
int main()
{
unsigned char* RAM = new unsigned char [0x1000];
for (int zero = 0; zero < 0x1000; zero++)
RAM[zero] = 0;
A a(RAM);
B b(RAM);
a.Set_B_Ptr( b );
b.Set_A_Ptr( a );
a.Run();
b.Run();
b.Update( a );
b.Update2();
delete [] RAM;
return 0;
}
Nephi
define A Object and B Object. A Object and B Object need to share
unsigned char array. They are very different object. They are like
two chips.
A Object modifies data in the unsigned char array. Then, B Object is
in turn to modify data in the unsigned char array. Static data member
is not the answer. Unsigned char array in the file scope is a bad
idea because other functions or classes might modify unsigned char
array accidently. The only way is to guard it inside main() function.
You need to define two pointers. Two pointers are used to share
unsigned char array. You have to be careful when you write your code
in the correct sequence.
Sometimes, you may want to copy data member from A Object to B
Object. Friend class is the answer. Please say if Update() function
or Update2() function is better. Update2() function may have
additional overhead because "this pointer" reads memory address
indirection before indirection memory address is read again to another
indirection memory address and then accesses data member. Update()
function uses reference to A Object.
Compare Run() function in both A Object and B Object.
Place NULL in unsigned char array in both ~A() function and ~B()
function are unnecessary.
Please state your opinion what you think my good design C++ code.
Here is an example of my code below.
class B;
class A
{
public:
A( unsigned char *RAM )
{
this->RAM = RAM;
this->a = 'A';
this->b = 'B';
this->c = 'C';
}
~A() { this->RAM = 0; }
void Set_B_Ptr(B &b)
{
this->B_Ptr = &b;
}
void Run()
{
this->RAM[0] = this->a;
this->RAM[1] = this->b;
this->RAM[2] = this->c;
}
private:
friend class B;
unsigned char* RAM;
unsigned char a;
unsigned char b;
unsigned char c;
B *B_Ptr;
};
class B
{
public:
B( unsigned char *RAM )
{
this->RAM = RAM;
this->d = 'D';
this->e = 'E';
this->f = 'F';
}
~B() { this->RAM = 0; }
void Set_A_Ptr(A &a)
{
this->A_Ptr = &a;
}
void Run()
{
this->RAM[3] = this->d;
this->RAM[4] = this->e;
this->RAM[5] = this->f;
}
void Update(A &a)
{
this->a = a.a;
this->b = a.b;
this->c = a.c;
}
void Update2()
{
this->a = this->A_Ptr->a;
this->b = this->A_Ptr->b;
this->c = this->A_Ptr->c;
}
private:
friend class A;
unsigned char* RAM;
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
unsigned char e;
unsigned char f;
A *A_Ptr;
};
int main()
{
unsigned char* RAM = new unsigned char [0x1000];
for (int zero = 0; zero < 0x1000; zero++)
RAM[zero] = 0;
A a(RAM);
B b(RAM);
a.Set_B_Ptr( b );
b.Set_A_Ptr( a );
a.Run();
b.Run();
b.Update( a );
b.Update2();
delete [] RAM;
return 0;
}
Nephi