I
Immortal Nephi
The base class has some data members. Tens of member functions or
hundreds of member functions modify data members. What if programmer
make mistakes? They might assign incorrect value to the data
members. Then, they have to examine all tens of member functions or
hundreds of member functions to try and locate incorrect values. They
waste their time. They have difficulties to find a bug.
Set and Get member functions are the answer. They can guard against
incorrect value in the data members. The programmers do not have to
examine all member functions. They can only locate set or get member
function before they can easily correct incorrect values. It saves
more time.
Can C++ Compiler inline set and get member functions to improve
optimization? Then data members should be set to private. Set and
Get member functions will be inherited into derived class.
Please take a look at my code. It gives you some ideas. Tell me
your opinion. Is it worth best C++ practice?
typedef unsigned char ubyte;
typedef unsigned short int uword;
class A
{
public:
A() : m_data( 0 ), m_flag( 0 ) {}
~A() {}
void set_data_low_byte( ubyte data )
{
m_data &= 0xFF00;
m_data |= data;
}
void set_data_high_byte( ubyte data )
{
m_data &= 0xFF;
m_data |= ( data << 8 );
}
void set_data_word( uword data )
{
m_data = data;
}
ubyte get_data_low_byte() const
{
return m_data;
}
ubyte get_data_high_byte() const
{
return m_data >> 8;
}
uword get_data_word() const
{
return m_data;
}
void set_flag( ubyte flag )
{
m_flag = flag & 1;
}
ubyte get_flag() const
{
return m_flag;
}
private:
uword m_data;
ubyte m_flag;
};
class B : public A
{
public:
B() {}
~B() {}
void F1()
{
uword data = 0xF0;
data += 0X30;
set_data_low_byte( data ); // Assign 0x20 to low byte and ignore
high byte
set_flag( 3 ); // Replace from 3 to 1 by default
}
void F2()
{
uword data = get_data_low_byte();
data += 0X430;
set_data_high_byte( data ); // Assign 0x50 to high byte
if( get_flag() )
set_flag( 0 );
}
void F3()
{
uword data = get_data_word();
data += 0x2C5;
set_data_word( data );
}
};
int main()
{
B b;
b.F1();
b.F2();
b.F3();
return 0;
}
hundreds of member functions modify data members. What if programmer
make mistakes? They might assign incorrect value to the data
members. Then, they have to examine all tens of member functions or
hundreds of member functions to try and locate incorrect values. They
waste their time. They have difficulties to find a bug.
Set and Get member functions are the answer. They can guard against
incorrect value in the data members. The programmers do not have to
examine all member functions. They can only locate set or get member
function before they can easily correct incorrect values. It saves
more time.
Can C++ Compiler inline set and get member functions to improve
optimization? Then data members should be set to private. Set and
Get member functions will be inherited into derived class.
Please take a look at my code. It gives you some ideas. Tell me
your opinion. Is it worth best C++ practice?
typedef unsigned char ubyte;
typedef unsigned short int uword;
class A
{
public:
A() : m_data( 0 ), m_flag( 0 ) {}
~A() {}
void set_data_low_byte( ubyte data )
{
m_data &= 0xFF00;
m_data |= data;
}
void set_data_high_byte( ubyte data )
{
m_data &= 0xFF;
m_data |= ( data << 8 );
}
void set_data_word( uword data )
{
m_data = data;
}
ubyte get_data_low_byte() const
{
return m_data;
}
ubyte get_data_high_byte() const
{
return m_data >> 8;
}
uword get_data_word() const
{
return m_data;
}
void set_flag( ubyte flag )
{
m_flag = flag & 1;
}
ubyte get_flag() const
{
return m_flag;
}
private:
uword m_data;
ubyte m_flag;
};
class B : public A
{
public:
B() {}
~B() {}
void F1()
{
uword data = 0xF0;
data += 0X30;
set_data_low_byte( data ); // Assign 0x20 to low byte and ignore
high byte
set_flag( 3 ); // Replace from 3 to 1 by default
}
void F2()
{
uword data = get_data_low_byte();
data += 0X430;
set_data_high_byte( data ); // Assign 0x50 to high byte
if( get_flag() )
set_flag( 0 );
}
void F3()
{
uword data = get_data_word();
data += 0x2C5;
set_data_word( data );
}
};
int main()
{
B b;
b.F1();
b.F2();
b.F3();
return 0;
}