Mail Archives: djgpp/1998/06/09/20:30:25
>//In foo.h
>
>class foo
>{
> public: foo();
> protected:
> int foosint;
>};
Good so far...
>//in foo.cpp
>
>#include "foo.h"
>
>foo::foo()
>{
> foosint = 0;
>}
Not really important, but a "better" (and of course better is hugely relative)
way to do this is:
foo::foo()
: foosint(0)
{
};
Why is this better? Well pretend you had an element in class foo which did not
have a default constructor. For instance bas( char* S ). You would want to
initialize bas with some string then, inside foos constructor, but this will
not work as you expect:
foo::foo()
{
mybas = bas( "hi" );
};
Because what happens is the compiler stops when it sees it can't generate
bas() without a string as an argument. And it of course must generate objects
before you can use any code which manipulates them. So you either have to
write a default ctor for bas (actually not a bad idea, I usually/almost always
do anyways) or do this:
foo::foo()
: mybas( "hi" )
{
};
Er, but anyways, that's nothing to do with what you were asking...
>//in bar.h
>
>#include "foo.h"
>
>class bar : public foo
>{
> public: foo();
>};
Why are you overloading foo()? Don't you mean bar()? I think that is your
problem. foo() gets called automatically when you create bar() due to the
inheritance (well I *think*, best to check it out, but I'm quite sure, grr so
many things to remember w/ C++)
>//in bar.cpp
>
>#include "bar.h"
>
>bar::bar()
>{
> foosint = 4;
>}
>
>This creates the error:
>
>Error: member 'foosint' is a protected member of class 'foo';
>
>Somewhere in the multiple files, the compiler lost the inheritence.
Well, I expect what is happening is the compiler does not see bar::bar() as a
function of bar, so it doesn't pick up on the inheritance (i.e., it just sees
it as another function, and possibly namespaces make this kind of thing
perfectly legal, I'm not really sure) But you have the inheritance right. And
don't worry, djgpp will not "lose" inheritance in multiple files, though you
may run into trouble yet there... Let me know if that doesn't fix it,
- Calvin -
>By the way, I use DJGPP w/ RHIDE.
>
>Any help welcome.
>
---
http://www.cadvision.com/frenchc/ (Wig out, peeps.)
B013CD10B7A08EC3B2C8B940018BC133C2AAE2F9FECA75F2B407CD21B80300CD10C3
- Raw text -