How to encapsulating Object?

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::pixels);

return 0;
}

Nephi
 
N

Noah Roberts

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?

singleton
 
J

James Kanze

singleton

Maybe. His first sentence is "Sometimes, programmers decide to
create only one object"; if the client programmer decides to
create only one object, then he only creates one object, and
that's it. A singleton is used if the abstraction of the class
is such that it doesn't make sense to create more than one
object (and that object should have the lifetime of the
program).
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
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.

Do you mean functions that directly use the global ?
I rather use the global as default parameters. That way, I can apply the
same function locally:

<from your example>
void Modify_RGB(unsigned int& R=Red
,unsigned int& G=Green
,unsigned int& B=Blue)
{...
The namespace is the only way to encapsulate global variable and global
function.

You can also do it in a class.
<from your example>
struct Color
{
static unsigned int Red = 0;
static unsigned int Green = 0;
static unsigned int Blue = 0;

static unsigned int RGB = 0;

static void Modify_RGB()...

This add a few restrictions: no alias with using and you cannot add
extend the 'namespace' outside the point of definition.

This can be useful if you want to ensure nobody will pollute your namespace.
The source code is much easier to be readable if namespace
is used.

This has been debated and some don't use namespace because they think it
adds complexity relatively to the scale of their project.

And koenig lookup can yield oddities in some specific case.
Multiple modules can share global varable and global
function without any confusion.

With your example, they might have some multithreading issues which is
often the case nowadays.

I don't see where the confusion would come. You can also initialize a
module by providing it the context and not allow it to directly use the
global; it is IMHO cleaner.
They can create dynamic linked library to include global variable and
global functions. The clients can reuse library.

Globals/Singletons within a dynamically loaded library are more an added
complexity than an asset; particularly when unloading.
If programmers decde to create more than one object on a whole
program, class is the answer.

You can define a class and then use one instance as a global. Singletons
are useful if you want to ensure there can be only one instance of the
class but it is rarely necessary.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top