473,509 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Write two separated objects?

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
Jun 27 '08 #1
7 1141
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.
Jun 27 '08 #2
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
Jun 27 '08 #3
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 help to separate objects?

Bryan Parkoff
Jun 27 '08 #4
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
Jun 27 '08 #5
[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.
Jun 27 '08 #6
Ian Collins wrote:
...Namespaces do not exist in C.
True. "Name spaces" on the other hand...

--
Peter
Jun 27 '08 #7
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
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6315
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
27
5626
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
6
1456
by: Shimon Sim | last post by:
I am writing a control and need to introduce custom nested property. I need it to look exactly as XXXStyle properties for DataGrid. I starting looking into TypeConverters and it seems that I need...
3
11257
by: Neal Miller | last post by:
Hi all I've gotten this error: Err# 5 COM object that has been separated from its underlying RCW can not be used I have researched this some, and have discovered that it has something...
11
9351
by: muttu2244 | last post by:
hi everybody i want to write a set of values to a file from python. For ex:: the fields name will "comp name", "ip addr", "mac addr" etc. And below all these fields i ll have the values for...
26
2791
by: Martin Jørgensen | last post by:
Hi, I'm learning C-programming. I have a program which I would like to modify so it takes arguments from the commandline. Let call the program: program.exe. Could somebody shortly explain how...
2
3357
by: stefan.moser | last post by:
Hi All, I'm having a problem implementing the Separated Interface pattern from Martin Fowler's book Patterns of Enterprise Application Architecture. I'm using C# in Visual Studio 2005. The...
62
4359
by: robert | last post by:
I'd like to use multiple CPU cores for selected time consuming Python computations (incl. numpy/scipy) in a frictionless manner. Interprocess communication is tedious and out of question, so I...
4
14260
by: Michael Maes | last post by:
Hi, On closing our App, we get following 'InvalidComObjectException': "COM object that has been separated from its underlying RCW cannot be used" The App has been upgraded from .Net1.1 to...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.