Share Global Variable & Constant Variable

Discussion in 'C++' started by Immortal Nephi, Aug 17, 2009.

  1. 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;
    }
    Immortal Nephi, Aug 17, 2009
    #1
    1. Advertising

  2. Immortal Nephi wrote:
    > 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;
    > }
    Vladimir Jovic, Aug 17, 2009
    #2
    1. Advertising

  3. Immortal Nephi

    Jonathan Lee Guest

    On Aug 16, 7:02 pm, Immortal Nephi <> wrote:
    >         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
    gain in performance, the const_cast<> is more cumbersome
    than setter notation, and you've lost a layer of protection.

    > 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
    Jonathan Lee, Aug 17, 2009
    #3
  4. Immortal Nephi

    Technical Guest

    On Aug 16, 7:02 pm, Immortal Nephi <> wrote:

    >         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
    Technical, Aug 18, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,068
  2. Saraswati lakki
    Replies:
    0
    Views:
    1,275
    Saraswati lakki
    Jan 6, 2012
  3. Jack
    Replies:
    2
    Views:
    192
  4. Eric Wong
    Replies:
    0
    Views:
    188
    Eric Wong
    Feb 13, 2010
  5. Alan
    Replies:
    2
    Views:
    117
Loading...

Share This Page