Sign in to follow this  

Inheritance

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

lets say i have 2 classes Class Graphic Class Resource how can i derive them so both classes has acces to each others protected variables like Class Graphic : public Resource Class Resource : public Graphic but yea it might be impossible ill maybe make one class of them instead another problem is this lets say i have 2 classes ( hehe again) Class Window Class Graphic : public Window but when i tryed to use any of the protected variables in the Window class from my graphic class the variables is NULL or 0x0 why?? thx hunter_Ex

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
did you initialize the window variables in the constructor for graphic?
i.e.

public:
graphic(void) : window() { }
...

Share this post


Link to post
Share on other sites
i maked the window and stuff in the Window Class

like

[SOURCE]
Class Window
{
private :
protected :

HWND window;

public:

construct
deconstruct
etc...
}

Class Graphic : public Window
{
// wanna use the window handle but i only get a NULL result when i uses it
even if a window is created in the "window" variables why

i tryed manually get the window from a function from window and supply it to the graphic class and then it worked any clue??

}


[/SOURCE]


hunter_ex

Share this post


Link to post
Share on other sites
I'm not entirely sure what you're doing, but it sounds a bit like you're expecting an instance of the Graphics class to magically know about the data stored in a completely separate instance of the Window class.

Inheritance involves an 'is a' type relationship. If you derive Graphics from Window then an instance of the Graphics class will have all the functionality of the Window class in addition to it's own. However, the data for instances of these classes are all seperate, unless you're using static members.

Share this post


Link to post
Share on other sites
then whats wrong with this code :S



// this is only the header
class Window
{
private:

protected:

HWND window;

public:

inline void MakeWindow(char* caption) {
// psuedo code
create custom class
register class
createwindow
functions worked it created a window
};
};

class Graphic : public Window
{
public:

inline void CreateD3D() {
// psuedo code
CreateDevice(window);
};
};

// and now my little test file not a complete

#include <windows.h>
#include <d3dx9.h>
#include <the upper header.h>

Window form;
Graphic graph;

// here i start the Win Main ()
{
form.MakeWindow("test window);
// the window worked and the "window" variable is valid

// now set up d3d device
graph.CreateD3D();
// but this functions failes couse the derived functions "window" from class
//Window is NULL... WHY?


// etc.. the message handler
};






the window variable is declared and known in the graphic class but
the value is always NULL even if the "window" really is a window handle
with non NULL value

help plzz hunter_ex

[Edited by - Hunter_Ex on July 19, 2005 8:11:58 AM]

Share this post


Link to post
Share on other sites
Ah, that confirms what I suspected.

form and graph are two completely separate objects. form is a 'Window' object. graph is both a 'Window' and a 'Graphics' object. Both have a member variable called 'window' but this is not shared between them, they are two completely separate variables.

Instead of calling form.MakeWindow(), just call graph.MakeWindow() instead. You don't need to create a separate 'Window' object, graph is already a 'Window' object in it's own right. You don't need 'form' at all.

Share this post


Link to post
Share on other sites
thats sounds wierd i tryed to change
"form.MakeWindow()" into "graph.MakeWindow()" but that gives me error "unknown variable window" so it didnt help

but why cant the graphic class use variables from its derived class Window??

Share this post


Link to post
Share on other sites
You must remember that it's not "graph" that's derived from "form", it's the "Graphic" class that's derived from the "Window" class. Each time you create an object, be it a Graphic object or a Window object, you create a new HWND window inside that object that is different from every other window variable -- another Graphic object or another Window object.

What Sandman suggested is what you need to do, with one addition: move HWND window; from the protected section to the private section of the class. Protected members cannot be accessed by subclasses; private members can.

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
What Sandman suggested is what you need to do, with one addition: move HWND window; from the protected section to the private section of the class. Protected members cannot be accessed by subclasses; private members can.


Uh, no, it's the other way around: protected members can be accessed from subclasses, but private cannot.

Hunter_Ex: Just calling graph.MakeWindow() instead of form.MakeWindow should work fine. Are you sure you didn't change something else as well?

You seem to be having a bit of trouble with the concept of inheritance. Graphic *can* access Window's protected members. However 'form' and 'graphic' are two entirely separate objects, and have two completely separate member variables called 'window'.

Share this post


Link to post
Share on other sites
I might ask why you want to do that? If you have a Graphics object, you can do everything with it that you can with a Window object.

But if you have a Window object that is indeed only a Window object, not a Graphics object, then the proper thing to do is code access functions that you can use to tell the Graphics object about another Window object, and then they can communicate.

I hope that makes sense...

-Twilight Dragon

Share this post


Link to post
Share on other sites
Quote:
Original post by Hunter_Ex
isnt any way to make the graphic class use the window from the window class


Yes there is, you could make it static. But it's entirely the wrong thing to do in my opinion. I would strongly recommend against it. In fact, just don't do it. What you've got now, or TDragon's option above are much better.

However, one valid candidate for being a static member would be the atom returned from RegisterClassEx(). You could then check this variable, if it's NULL, you call RegisterClassEx(), if it's non-null, you just use it in your call to CreateWindowEx(), thus enabling you to create multiple windows of the same class.

e.g


// header
class Window
{
// blah
protected:

static ATOM classAtom;
// blah

};
// cpp
ATOM Window::classAtom = 0;



Share this post


Link to post
Share on other sites
right now it works

ive made the window class to take care of global variables like
window
device
etc...


so maked it as

Class Window
{
window
device
}

Class Resource : public Window
{
// take care of all loading of resource
}

Class Graphic : public Resource // take cares of resources and use them
{
// both this and resource class has acces to
// window & device ( works gr8 )
}

thx all

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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