I
Immortal Nephi
I would like to show you my code. Please tell me what you think.
If you place variable in the global scope, you are unable to find
which function modifies global variable. You want to use wrapper. I
have one problem.
Two classes have different objects. They need to share global
variable outside class. Think of writing game programming. Class A
is to be video object and class B is to be memory object. They need
to modify data in the shared memory address.
I decide to place global variable as g_data inside main function.
You put g_data into A’s constructor and B’s constructor.
Both of A’s constructor and B’s constructor copy g_data’s memory
address into m_data. Member data as m_data is always constant because
I do not want to modify memory address if I make mistake in C++
writing.
Please let me know what you think my code below. Shared global
variable in both classes is best practice.
I have discussed with folks in the newsgroups. Sometimes, they say
getters and setters are bad practice. Sometimes, I allow clients to
modify data members with getters and setters if necessary.
I hate to use getter only when data member is constant. I place it
in the public instead of private. I allow clients to read data
members instead of using getter function.
It is annoying to use getter functions too often when you want to
copy data member into your own variable. You appreciate to access
read only data member directly. Sometimes, C++ Compiler does not
optimize getters well.
If clients seldom must modify constant data member, then they use
const_cast. Please let me know your opinion. Should you use Getter
function outside class or access constant data member directly?
class A
{
public:
A( int &_data ) : m_data( &_data ), m_index( 0 ), x( 0 ), y( 5 ) {}
~A() {}
int Get_Data( const int _index ) const { return m_data[_index]; }
int Get_X() { return x; }
void Set_X( int _x ) { x = _x; }
private:
const int *m_data;
int m_index;
int x;
public:
const int y;
};
class B
{
public:
B( int &_data ) : m_data( &_data ), m_index( 0 ), x( 0 ), y( 5 ) {}
~B() {}
int Get_Data( const int _index ) const { return m_data[_index]; }
int Get_X() { return x; }
void Set_X( int _x ) { x = _x; }
private:
const int *m_data;
int m_index;
int x;
public:
const int y;
};
int main()
{
int *g_data = new int[ 10 ];
for( int i = 0; i < 10; i++ )
g_data = i;
A a( *g_data );
B b( *g_data );
int i1 = a.Get_Data( 1 );
int i2 = b.Get_Data( 2 );
a.Set_X( 5 );
int i3 = g_data[ a.Get_X() ];
int i4 = g_data[ a.y ];
int &Write_Y = const_cast< int& > ( b.y );
Write_Y = 7;
int i5 = g_data[ b.y ];
delete [] g_data;
return 0;
}
If you place variable in the global scope, you are unable to find
which function modifies global variable. You want to use wrapper. I
have one problem.
Two classes have different objects. They need to share global
variable outside class. Think of writing game programming. Class A
is to be video object and class B is to be memory object. They need
to modify data in the shared memory address.
I decide to place global variable as g_data inside main function.
You put g_data into A’s constructor and B’s constructor.
Both of A’s constructor and B’s constructor copy g_data’s memory
address into m_data. Member data as m_data is always constant because
I do not want to modify memory address if I make mistake in C++
writing.
Please let me know what you think my code below. Shared global
variable in both classes is best practice.
I have discussed with folks in the newsgroups. Sometimes, they say
getters and setters are bad practice. Sometimes, I allow clients to
modify data members with getters and setters if necessary.
I hate to use getter only when data member is constant. I place it
in the public instead of private. I allow clients to read data
members instead of using getter function.
It is annoying to use getter functions too often when you want to
copy data member into your own variable. You appreciate to access
read only data member directly. Sometimes, C++ Compiler does not
optimize getters well.
If clients seldom must modify constant data member, then they use
const_cast. Please let me know your opinion. Should you use Getter
function outside class or access constant data member directly?
class A
{
public:
A( int &_data ) : m_data( &_data ), m_index( 0 ), x( 0 ), y( 5 ) {}
~A() {}
int Get_Data( const int _index ) const { return m_data[_index]; }
int Get_X() { return x; }
void Set_X( int _x ) { x = _x; }
private:
const int *m_data;
int m_index;
int x;
public:
const int y;
};
class B
{
public:
B( int &_data ) : m_data( &_data ), m_index( 0 ), x( 0 ), y( 5 ) {}
~B() {}
int Get_Data( const int _index ) const { return m_data[_index]; }
int Get_X() { return x; }
void Set_X( int _x ) { x = _x; }
private:
const int *m_data;
int m_index;
int x;
public:
const int y;
};
int main()
{
int *g_data = new int[ 10 ];
for( int i = 0; i < 10; i++ )
g_data = i;
A a( *g_data );
B b( *g_data );
int i1 = a.Get_Data( 1 );
int i2 = b.Get_Data( 2 );
a.Set_X( 5 );
int i3 = g_data[ a.Get_X() ];
int i4 = g_data[ a.y ];
int &Write_Y = const_cast< int& > ( b.y );
Write_Y = 7;
int i5 = g_data[ b.y ];
delete [] g_data;
return 0;
}