Share Global Variable & Constant Variable

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;
}
 
V

Vladimir Jovic

Immortal said:
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;
};

This is awful design.
int main()
{
int *g_data = new int[ 10 ];

for( int i = 0; i < 10; i++ )
g_data = i;


What happens if someone do this here:
delete [] ( g_data );
?
A a( *g_data );
B b( *g_data );

int i1 = a.Get_Data( 1 );
int i2 = b.Get_Data( 2 );

What happens when someone do this :
a.Get_Data( 11 );
?
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;

This is an undefined behaviour. It is not allowed to change variables
declared as constants.
int i5 = g_data[ b.y ];

delete [] g_data;

return 0;
}
 
J

Jonathan Lee

        I have discussed with folks in the newsgroups.

In this very newsgroup, actually:

http://groups.google.com/group/comp...ff11c51f246/e844ae74a28283bc#e844ae74a28283bc
 Sometimes, they say getters and setters are bad practice.

The key word here is _sometimes_. I don't think the code
you posted is one of those times. There is no observable
It is annoying to use getter functions too often when you want to
copy data member into your own variable.

A) In what way is it annoying? The notation? Use the following
get/set notation:

private:
int internalDataname;
public:
int dataname() const; //getter
void dataname(int); //setter

The notation is very clean -- it only requires parentheses.
For the array, overload operator[].

B) As in the previous thread, there is a question of _why_
you're copying class data to "your own variable" all the
time. There are circumstances where it is appropriate,
but a redesign might also be appropriate...
 You appreciate to access read only data member directly.

Well, no, _you_ appreciate it.
 Sometimes, C++ Compiler does not optimize getters well.

Have you determined this through profiling? I would be highly
suspect of a program that spent more than a fraction of a
percent of its running time in getters. If you do have such a
case, it is the exception, not the rule.
If clients seldom must modify constant data member, then
they use const_cast.  Please let me know your opinion.

Nevermind the fact that it's going to be undefined behavior,
what if the user sets it to something invalid?

--Jonathan
 
T

Technical

        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.

The preferred fcrm offered up from hired (outside) help at my firm -
involves the use of a Myers singleton

class c_constants {
public :
double const PI ;
static int const MAX_INT = INT_MAX ;
c_constants()
: PI ( 3.14159 )
{}
static c_constants& instance() {
static c_constants inst ;
return inst ;
}
};

See below for more:

http://groups.google.com/group/comp...3d/3eeefe072b6318c?hl=en&q=ma740988+singleton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top