Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Object-oriented programming with C?

Status
Not open for further replies.

ltg

Member level 4
Joined
Feb 24, 2002
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
533
Hi C experts,

Could you please give me some tips or guidline how to write C code in object-oriented way? I will use the benefit from C++. Thanks in advance!
Best Regards,
ltg
 

ash

Full Member level 3
Joined
Dec 25, 2002
Messages
151
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Location
Europe
Activity points
1,051
What's your application and Why do you need oop for your application? There are lots of free c++ compilers. if you don't have c++ compiler for the platform which you use, that's different. Please give some information about your project.

Regards.
 

ltg

Member level 4
Joined
Feb 24, 2002
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
533
Hi,

Thanks for your response. Sorry for my unclear information. I use C for developing our embedded system projects. The processors are 8-bits so I can't use C++ compiler.

Best Regards,
ltg
 

ash

Full Member level 3
Joined
Dec 25, 2002
Messages
151
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Location
Europe
Activity points
1,051
Hi ltg,
It's possible but the result isn't satisfactory. Especially in embedded applications, size and also speed is vital. Memory size of mcu isn't enough for oop. As i said it's possible but unuseful, if you're interested, i'm sending a link.

https://www.gmonline.demon.co.uk/cscene/CS1/CS1-02.html

Regards.
 

glitch

Newbie level 6
Joined
Jul 31, 2002
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
56
ltg:

There are embedded C++ compilers as well.. take a look at the IAR embedded workbench offerings. But I do agree that you may be taking a unnecessary performance penalty as well as additional memory requirements. What aspects of C++ are you looking to use?

for simple data, use structures
struct myobject {
int var1;
char var2;
};
then pass it to the functions under a parameter called 'this' (this is basically how c++ does it behind the scenes)

void myobject_function(myobject this, int param1, int param2);

you will not be able to do operator overloading. But you can do virtual functions and function overloading with some careful coding. To do this basically include a function pointer in the structure, that will point to the appropriate version of the function. If you need to inherit from another 'class' create a member called 'super' that references the parent type. unfortunately all of the members of 'super' won't be directly accessable you will need to use the additional level of indirection to access them.

HTH

[edit: Ash's link explains all this much better than i did ;) ]
 

Antharax

Full Member level 4
Joined
Dec 31, 1999
Messages
208
Helped
14
Reputation
28
Reaction score
1
Trophy points
1,298
Activity points
2,865
I think object oriented programming for embedded systems should be done VERY carefully!
dynamic memory allocation is very dificult to manage on an uC with limited memory!!! It's hard to write no mistakes even with static allocation, even when your compiler does most of the work. On an embedded system you probably aren't running an operating system with powerfull memory management. (even Windows isn't able to do it correctly all the time)

so don't use new or deletes ;-)
Classes might be usable when you allocate everything staticaly but they might create allot of overhead (especialy don't use virtual functions, ...)

Antharax
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
Forget C++ for embeded systems, C++ is designed for very large software systems, where you need namespaces and interfaces, vtables, virtual functions, and all the other bullshit.
If you think C++ is not overly complicated, just what is a 'protected abstract virtual base pure virtual
private destructor', and when was the last time you needed one?
What you need is Structured C programming. Divide your program into files that serve one purpose. Hide fuctions with the static keyword. Use structures for data. C is very powerful and elegant language. Unix os is written in C. Thats stood the test of time!
 

stanleysan

Junior Member level 1
Joined
May 23, 2003
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
121
if you want to make your code more flexibilty.
you must use more time design it then coding .
however interface is most important to you.
in these domain ,there are some patterns already,for example , check the book ---- "small memory software".
 

Rus

Newbie level 6
Joined
Mar 28, 2003
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
74
there are c++ compilers for 8-bit cpus too, anyway you may write an oop programs even on asm.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top