|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. The same global variable and global function will be in two separated header / source codes. The main source code includes two headers. One problem is that main source code can't access the same global variable name and global function name in both headers. It is only the solution if you want to name two separated global scopes. C++ Compiler has the feature to use namespace keyword. How would you suggest to write this way using C Compiler? It should look like this below. namespace Blue { int a; int b; int Run_Color (void); } namespace Red { int a; int b; int Run_Color (void); } int main (void) { Blue::a = 10; Blue::b = 20; Blue::Run_Color (void); Red::a = 2; Red::b = 4; Red::Run_Color (void); Return 0; } If it does not have the solution, then write a separated function name using underscore like this - Blue__Run_Color (), Red__Run_Color (). Blue__a = 2; Red__a = 10. It does not look neat design. What do you recommend? Yours Truly, Bryan Parkoff |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Bryan Parkoff wrote:
> 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); > }; > If you want OO style in C why not simply write typedef struct Colour Colour; struct Colour { int a; int b; int (*run)(Colour*); }; and assign the runColor function at run time? int runRed(Colour*); int runBlue(Colour*); int main() { Colour red; red.run = runRed; Colour blue; blue.run = runBlue; red.run( &red ); blue.run( &blue ); } > > I am not going to use C++ Compiler. I stick C Compiler for > critical performance reason. Which is? -- Ian Collins. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Bryan Parkoff wrote:
> 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> <snip> > C++ Compiler has the feature to use namespace keyword. C has namespace, but it's subject to manual use and maintenance... /* red.h */ struct red { int a; int b; }; int red_Run_Color (struct red *thiz); /* blue.h */ struct blue { int a; int b; }; int blue_Run_Color (struct blue *thiz); /* main.c */ #include "red.h" #include "blue.h" int main(void) { struct blue blue = { 10, 20 }; struct red red = { 2, 4 }; blue_Run_Color(&blue); red_Run_Color(&red); return 0; } -- Peter |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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!! 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. Global variable and global variable do not need pointer. The x86 instructions are reduced to exclude pointer so critical performance can be improved. > <snip> >> C++ Compiler has the feature to use namespace keyword. > > C has namespace, but it's subject to manual use and maintenance... > > /* red.h */ > struct red > { > int a; > int b; > }; > > int red_Run_Color (struct red *thiz); > > /* blue.h */ > struct blue > { > int a; > int b; > }; > > int blue_Run_Color (struct blue *thiz); > > /* main.c */ > #include "red.h" > #include "blue.h" > > int main(void) > { > struct blue blue = { 10, 20 }; > struct red red = { 2, 4 }; > > blue_Run_Color(&blue); > red_Run_Color(&red); > > return 0; > } 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; } Do you see my code above? It is neat design using namespace keyword and double colon. It is a lot of easier to access global variable and global function when you are very careful inside namespace block. How do I know if C Compiler support namespace keyword? Do you think that namespace to separate objects? Bryan Parkoff |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 to separate > objects? Opinion is divided. All I know is that C doesn't give you much of a choice beyond manual nomenclature. -- Peter |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
[please don't snip attributions]
Bryan Parkoff wrote: > Peter Nilsson wrote: >> <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!! 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. Global > variable and global variable do not need pointer. The x86 instructions are > reduced to exclude pointer so critical performance can be improved. > What are you talking about? What pointer? struct Red { int a, int b }; struct Red red; red.a = 0; Where's the pointer? > How do I know if C Compiler support namespace keyword? You don't have to, it won't. Namespaces do not exist in C. -- Ian Collins. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Ian Collins wrote:
> ...Namespaces do not exist in C. True. "Name spaces" on the other hand... -- Peter |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Bryan Parkoff said:
<snip> > > How would you suggest to write this way using C Compiler? Why not start off by telling us the problem you're actually trying to solve? You have not yet done this. Instead, you've told us your C++ solution, and you've asked us for a translation. That's the Wrong Way. Having decided on C rather than C++, you need to construct a C solution from scratch. If you translate from C++ to C, you'll get something that has few if any of the benefits of your C++ design, and few if any of the benefits you could have enjoyed if you'd embraced C idioms right from the start. > It should look like this below. > > namespace Blue This is a clear indication that you are still thinking in C++. If you want a C solution, write a C solution rather than a C++ solution. <snip> > It does not look neat design. What do you recommend? I recommend starting with a problem definition. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
![]() |
| Outils de la discussion | |
|
|