|
-
September 24th, 2011, 07:52 AM
#1
Inherintance or Variable
Hello,
I'm writing a win32 class wrapper for fun and I've got a situation where I'm not sure what to do. I'll show a little bit code for example
Code:
class Window : public base // Base is a class where the HWND is stored and Hide() function etc
{
// stuff
}
class Button : public base // same as bove
{
// stuff
}
This code is all in the library
Now when I use my lib I'm making a class where window is public. The problem I've is should I make a variable of Button or should I make button public for the window class like this:
Code:
class Window : public base, public button
{
// stuff
}
class myclass : public Window
{
// stuff
}
else I've to do to in myclass this, Button * button1;
Now I only showed the button class, but what if I also use a listview,editbox, etc classes. If I included them all in Window class it will be huge. Should I do this or not? Also does the compiler leave out the code which the class do not need. For instance when myclass only the use the button class from Window, the rest got inculded to?
Thanks for reading,
Gz
-
September 27th, 2011, 04:10 AM
#2
Re: Inherintance or Variable
A class which encapsulates a button plus a lisbox plus an edit box and so on, has no practical sense.
So, just use simple inheritance when dealing with windows.
// see my signature.
Last edited by ovidiucucu; September 27th, 2011 at 04:14 AM.
Reason: typo
-
September 27th, 2011, 07:41 AM
#3
Re: Inherintance or Variable
 Originally Posted by ovidiucucu
A class which encapsulates a button plus a lisbox plus an edit box and so on, has no practical sense.
example...
Code:
class cMainWindow
{
cWindow Window;
cButton SaveButton;
cButton CloseButton;
cListBox DataListbox;
public:
cMainWindow() : Window(), SaveButton( Window ), CloseButton( Window ), DataListbox( Window )
{
Window.Show();
}
}
That makes sense to me. I think you meant:
A class which inherits a button plus a lisbox plus an edit box and so on has no practical sense.
Of course one does use encapsulation with inheritance by the use of access specifiers.
 Originally Posted by NLscotty
The problem I've is should I make a variable of Button or should I make button public for the window class like this:
Code:
Code:
class Window : public base, public button { // stuff } class myclass : public Window { // stuff }
else I've to do to in myclass this, Button * button1;
I believe you think that if you inherit multiple types( button, listbox ), you're generating a class which utilizes that type as an object and that is wrong. You effectively made your new class behave as if it were all of them combined. Now consider trying to inherit another button :|. You will see that your class already behaves like a button and it can't behave like a button again.
You are misusing inheritance in a way to express your idea that is incorrect down to the fundamentals of c++, so how can you include 2 buttons with inheritance? Obviously you can't. You are mixing up inheritance with objects. With objects, you can have multiple buttons.
NLscotty, please understand that creating your own wrapper takes extensive understanding in not only c++, but win32. Let's not forget about proper design. This can take years for someone who is just starting at your fundamental level.
If I included them all in Window class it will be huge. Should I do this or not?
The more you inherit, the bigger your class will get. Right now I wouldn't worry about false optimization nor would I support your current design. I would recommend you look at the code above.
Also does the compiler leave out the code which the class do not need. For instance when myclass only the use the button class from Window, the rest got inculded to?
Usually the compiler will remove functions that aren't used.
The best ideal you can try is:
1. learn the difference from inheritance and objects
2. learn c++ first with oop design in mind
3. learn from other widgets and how to improve on them. for ex, how does mfc pipe messages? Does it support multithreading?
4. learn win32 separately
Again a window is not a button and a listbox and a combobox nor should it be implemented like a button and a listbox and a combobox.
Also multiple inheritance can be a good thing if used correctly.
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
September 28th, 2011, 06:39 AM
#4
Re: Inherintance or Variable
 Originally Posted by Joeman
I think you meant:
A class which inherits a button plus a lisbox plus an edit box and so on has no practical sense.
Just was formally talking in other words.
 Originally Posted by Joeman
Also multiple inheritance can be a good thing if used correctly.
Not in the discussed case.
Here is scratching with the foot and violates the KISS principle.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|