Should I learn C++?

Programming, for all ages and all languages.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

I've decided to start my OS from scratch (again) but this time I am using C++ and, with a bit of tweaking, works quite well. I would recommend, however, that you try and get the basics working in C first, because imho it is better suited to learning OS development. You also need a good knowledge of C++ to try and get it working.

I find my source looks cleaner in C++, and it brings several other benefits. Templates are useful for defining basic data structure types like lists and trees, which I use extensively. I also find object inheritance really useful in making my OS portable. For example, I could declare a physical memory manager as:

Code: Select all

class PhysMemAllocator {
public:
  physaddr_t Alloc() = 0;
  physaddr_t Alloc(physaddr_t fixed_addr) = 0;
  void Free(physaddr_t) = 0;

  ~PhysMemAllocator() {};
};
and then derive from it to support various different architectures, e.g. i386_PhysMemAllocator, i386pse_PhysMemAllocator, x86_64_PhysMemAllocator, arm_PhysMemAllocator etc, which will return different page sizes, some may have bitmap support for the bottom 16mb for DMA and so on.

If I then link all the architecture code into a library for that arch, I can then just include one in my final image, along with the 'core' library, and everything is set up as needed. I can use a similar method for a kernel console class, whether it outputs to vga or a serial port, for example.

Whilst all of this is doable in c, I find that C++ better lends itself to this sort of thing. For anyone interested, I find the wiki is an excellent resource, particularly CPlusPlus and the C++ bare bones.

Regards,
John.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

Is there any API's out there written in C++ that handle C and other lower level stuff specifically designed for kernel/OS development? Or would it be better just to create your own?
Post Reply