gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > C++ Inheritance

#138604 - Zenix - Sun Aug 26, 2007 1:51 pm

Hey all,

I'm new to DS development, so go easy.

A simple question, is it possible to use inheritance with the DS (libnds)?

I tried the following code,

Code:
class foo
{
public:
   int i;
};

class bar : foo
{
};

int main( void )
{
   bar *a = new bar;

   a->i = 3;

   delete a;

   return 0;
}


I'm sure this is valid C++ (it's late, so I might be mistaken), but it results in the following error,

Code:
f:/Prog/CPP/ds/DrunkenYarr/DrunkenYarr/arm9/source/main.cpp: In function 'int main()':
f:/Prog/CPP/ds/DrunkenYarr/DrunkenYarr/arm9/source/main.cpp:9: error: 'int foo::i' is inaccessible
f:/Prog/CPP/ds/DrunkenYarr/DrunkenYarr/arm9/source/main.cpp:20: error: within this context


Any help with this would be greatly appreciated.

#138605 - REEPER - Sun Aug 26, 2007 2:22 pm

I'm a complete newbie when it comes to ds development and even when it comes to c++ programming but your problem seems to lie with the fact that you haven't declared the class bar to publicly inherit the class foo, so its members remain private to the subclass bar

you should replace

Code:

class bar : foo
{
};


with

Code:

class bar : public foo
{
};

#138631 - Zenix - Mon Aug 27, 2007 12:26 am

Jeez, just go and make me look like an idiot :)

Cheers for the help, I blame it on being tired, and the fact that we're forced to use Java at Uni.

#138669 - PeterM - Mon Aug 27, 2007 3:50 pm

Uh oh, going from Java to C++ is likely to get you caught in many of C++'s "gotchas". One of which:
http://blogs.msdn.com/oldnewthing/archive/2004/05/07/127826.aspx
you could fall for if you were deleting a pointer to foo instead of bar.

You probably want to read the c++ faq lite, and pick up the Scott Meyers and Herb Sutter books if you can.
_________________
http://aaiiee.wordpress.com/

#138820 - Zenix - Wed Aug 29, 2007 11:29 am

Cheers for the tips.

I'm not actually new to C++, just have had to take a break from it while studying Java at Uni.

Zenix.