Sign in to follow this  
tama9836

Problem about class inheritance in C++

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 this post


Link to post
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 this post


Link to post
Share on other sites
Thx 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??

Share this post


Link to post
Share on other sites
Quote:
Original post by tama9836
Thx 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 this post


Link to post
Share on other sites
Quote:
Original post by wrice
How about this:

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


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this