I
Immortal_Nephi
Sometimes, programmers decide to create only one object. One object
is used for a whole program. You can run two or more programs at the
same time. Each program has a copy of its own object.
They decide to use global variable and global function. The
namespace is the only way to encapsulate global variable and global
function. The source code is much easier to be readable if namespace
is used. Multiple modules can share global varable and global
function without any confusion.
They can create dynamic linked library to include global variable and
global functions. The clients can reuse library.
If programmers decde to create more than one object on a whole
program, class is the answer.
Take a look at my example code. Please let me know if my source code
looks good design. If only one object is ued, should I use global
variables and globa functions or struct with global function?
// Main.cpp
namespace Color
{
unsigned int Red = 0;
unsigned int Green = 0;
unsigned int Blue = 0;
unsigned int RGB = 0;
void Modify_RGB()
{
Red += 10;
Green += 10;
Blue += 10;
RGB = (Red << 8) | (Green << 4) | Blue;
}
struct Pixels
{
unsigned int Red;
unsigned int Green;
unsigned int Blue;
};
Pixels pixels; // Already defined in file scope.
void Zero_RGB(Pixels &pixels)
{
pixels.Red = 0;
pixels.Green = 0;
pixels.Blue = 0;
}
}
int main()
{
Color::Red = 25;
Color::Green = 50;
Color::Blue = 75;
Color::Modify_RGB();
Color::Zero_RGB(Color:ixels);
return 0;
}
Nephi
is used for a whole program. You can run two or more programs at the
same time. Each program has a copy of its own object.
They decide to use global variable and global function. The
namespace is the only way to encapsulate global variable and global
function. The source code is much easier to be readable if namespace
is used. Multiple modules can share global varable and global
function without any confusion.
They can create dynamic linked library to include global variable and
global functions. The clients can reuse library.
If programmers decde to create more than one object on a whole
program, class is the answer.
Take a look at my example code. Please let me know if my source code
looks good design. If only one object is ued, should I use global
variables and globa functions or struct with global function?
// Main.cpp
namespace Color
{
unsigned int Red = 0;
unsigned int Green = 0;
unsigned int Blue = 0;
unsigned int RGB = 0;
void Modify_RGB()
{
Red += 10;
Green += 10;
Blue += 10;
RGB = (Red << 8) | (Green << 4) | Blue;
}
struct Pixels
{
unsigned int Red;
unsigned int Green;
unsigned int Blue;
};
Pixels pixels; // Already defined in file scope.
void Zero_RGB(Pixels &pixels)
{
pixels.Red = 0;
pixels.Green = 0;
pixels.Blue = 0;
}
}
int main()
{
Color::Red = 25;
Color::Green = 50;
Color::Blue = 75;
Color::Modify_RGB();
Color::Zero_RGB(Color:ixels);
return 0;
}
Nephi