# Problem about class inheritance in C++

This topic is 4480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Dear all, as the title said, my question is about class inheritance in C++ First of all, here is my original program design class a is the parent of class b The definition of class a is: class a: { protected: ..... //variables ..... public: ..... //functions ..... }; The difinition of class b is: (There is a enum typedef) typedef enum {apple, orange........} FRUIT; class b : public a { private: FRUIT fruit; ..... //variables public: ..... //function ..... }; As the program runs, fruit will be changed(will be changed inside class b's function) if the program is written in this way, it's ok. However, now i want to rewrite it in the way that both class a and b can access to "fruit", so i simply do that: The new program design: The definition of class a: typedef enum {apple, orange........} FRUIT; class a: { protected: FRUIT fruit; ..... //variables public: ..... //functions ..... }; The definition of class b: class b : public a { private: ..... //variables ..... public: ..... //function ..... }; The reason i write in this way is that i hope all the variable will be inherited to class b, so that both class a and b can access to "fruit"...but problem comes...fatal error exist, after i compile it(use VC6) and run it, a window pop up (the one say the program has error and has a don't send button) And i have used the debugger to check it, and it says there is access violation.. This problem has been annoying me for a long time....and what can i do for solving this question..??

##### Share on other sites
maybe try it once without the typedef:
// typdef enum {apple, orange........} FRUIT;-> enum FRUIT {apple, orange........};

##### Share on other sites
Your problem is runtime access violation.

The syntax of your code is not wrong.

I'm not sure that you used the variable, fruit, after initialized.

##### Share on other sites
This is a runtime error - it has nothing to do with the compilation. You class design is good enough, the error lies in your code (you are probably dereferencing a pointer that is not initialized or maybe you overwrite something in memory, and so on. There is an enormous quantities of possible reasons for this kind of crash, none of them are related with class inheritance).

Anyway, we are still able to help you: first, you have to debug your code. Since you are using VC++6, it should be easy - start the program in debug mode, add a breakpoint and run your program one step at a time until it crash). If you still don't understand what happens, you are allowed to post a small code sample that should contain the offending code and we'll try to correct the probem with you :)

BTW, you don't have to typedef enums in C++:

enum FRUIT { ... };

Works like a charm :)

Regards,

##### Share on other sites
However the problem has not been solved yet, I have tried to use the debugger to trace the program in order to monitor which part of program is going wrong, but the only thing i can see is a window is pop up(titled with disassembly)with a lot of assembly inside.
I have used debugger before, but i havent encountered this problem(using F9 to set break point and press F10 to trace the program step by step), is that a problem of project setting? or sth else??

##### Share on other sites

class a:
{
protected:
a() : fruit(apple) {} // !!!! insert here.
FRUIT fruit;
.....

##### Share on other sites
Quote:
 Original post by tama9836Thx for yours answers~~However the problem has not been solved yet, I have tried to use the debugger to trace the program in order to monitor which part of program is going wrong, but the only thing i can see is a window is pop up(titled with disassembly)with a lot of assembly inside. I have used debugger before, but i havent encountered this problem(using F9 to set break point and press F10 to trace the program step by step), is that a problem of project setting? or sth else??

maybe you can post your code?

##### Share on other sites
Quote:

It's never wrong to properly initialize your members, but not initializing an enum (which is effectively an int) shouldn't cause the program to crash.

• 47
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631374
• Total Posts
2999647
×