Bryan Parkoff wrote:
Peter,
Can you please advise me how to write two separated
objects? Sometimes, two objects have the same variable
name and function name, but they have different
algorithm to perform. The C++ Compiler allows you to
create two classes. Two classes bind their own local
variables and local functions.
>
For example:
>
Class Blue
{
int a;
int b;
int Run_Color (void);
};
>
Class Red
{
int a;
int b;
int Run_Color (void);
};
>
int main (void)
{
Blue blue;
Red red;
>
blue.a = 10;
blue.b = 20;
blue.Run_Color ();
>
red.a = 2;
red.b = 4;
red.Run_Color ();
>
return 0;
}
>
I am not going to use C++ Compiler. I stick C
Compiler for critical performance reason.
<OT>You're unlikely to find a _critical_ difference in what
you've mentioned so far. In fact, the difference in
maintenance would be one reason to stick to C++.</OT>
Well, if you use class, the class requires a pointer
always!!
In what sense? And why do you care? [Have you profiled the
code?]
You define Red and give variable name red. red uses a pointer
to locate memory address before it starts to access local
variable or local function.
Every object has an address in C (and C++). Every function
(local or otherwise) has an address in C. Implementations
are generally capable of optimising out actual object
allocation allocations.
Global variable and global variable do not need pointer.
The x86 instructions are reduced to exclude pointer so
critical performance can be improved.
I think you're needlessly getting hung up on implementation
details. From what I've seen static duration objects are
accessed the same way internally for both C and C++ (in
all the bundled C and C++ implementations I've seen.)
It's much harder for implementations to optimise static
duration objects because it's harder to determine where
they (will or won't) be used.
Namespaces, as their name implies, are purely constructs
for speparating names. They are syntactical sugar. They
don't affect performance. [Except possibly in an interpreter
with really bad name lookup. ;-]
<snip>
C++ Compiler has the feature to use namespace keyword.
C has namespace, but it's subject to manual use and
maintenance...
Perhaps I was too literal. There is no 'namespace' keyword
in C. There are namespaces, however which one your declaration
affects in C is determined only by context. You cannot override
it or create additional namespaces.
<snip>
Your example code above is ideal, but I want to comment.
/* red.h */
namespace Red
{
int a = 0;
int b = 0;
int Run_Color (void);
}
/* blue.h */
namespace Blue
{
int a = 0;
int b = 0;
int Run_Color (void);
}
#include "red.h"
#include "blue.h"
int main (void)
{
Red::a = 2;
Red::b = 4;
Red::Run_Color ();
Blue::a = 2;
Blue::b = 4;
Blue::Run_Color ();
return 0;
}
If that's what you wanted then (one) C equivalent is...
/* red.h */
int red_a;
int red_b;
int red_Run_Color (void);
/* blue.h */
int blue_a;
int blue_b;
int blue_Run_Color (void);
/* main.c */
#include "red.h"
#include "blue.h"
int main(void)
{
blue_a = 2;
blue_b = 4;
red_a = 2;
red_b = 4;
blue_Run_Color();
red_Run_Color();
return 0;
}
>
Do you see my code above? It is neat design using
namespace keyword and double colon.
It's a terrible design IMHO. I prefer to avoid 'globals'
as much as possible. They have their uses, but I would
never use them to fix 'performance' issues. In fact,
their use tends to degrade performance.
It is a lot of easier to access global
variable and global function when you are very careful
inside namespace block.
I'm afraid this makes no sense to me.
How do I know if C Compiler support namespace keyword?
No conforming C compiler is required to support namespace
as a keyword. C++ on the other hand does. I think you
should go back to a C++ group and describe what it is
you're actually trying to do. It sounds like a singleton
class.
Do you think that namespace help to separate
objects?
Opinion is divided. All I know is that C doesn't give you
much of a choice beyond manual nomenclature.
--
Peter