Easy Class Question
OK I have this:
class junk
{
protected:
int x;
}
class junk2 : protected junk
{
}
When I create junk and junk 2, is the value of x the same in both or does creating an inherited class create a new seperate x for junk2.
OK I figured out that this is true. What I need to know is if there is someway to do this: I have an inherited class, but I need to use the values of certain variable of the parent class. I don''t want to create new empty variables-I want to use the ones that are in the parent allready-Is this possible?
Just tried static and it won''t work-the item from parent I need to keep the way it is is a DirectDraw structure called lpdd. Any other ideas?
Here, desing your class this way, should work.
now, m_iX is available to class B as well.
Hope this helps.
class A{protected: int m_iX;public: A(); virtual ~A();};class B : public A{};
now, m_iX is available to class B as well.
Hope this helps.
OK in your example:
A.M_iX=3;
Now B.M_iX does not = 3, I need to access the value of A.M_iX from within the inherited class. I have a function in B that needs to know what the value of A.M_iX, but its not an integer-its actually a structure and static will not work-any other ideas?
A.M_iX=3;
Now B.M_iX does not = 3, I need to access the value of A.M_iX from within the inherited class. I have a function in B that needs to know what the value of A.M_iX, but its not an integer-its actually a structure and static will not work-any other ideas?
It sounds like you''re doing this:
class A
{
protected:
int x;
};
class B : public A
{
}
and then creating an instance of class A
A cA;
and then creating an instance of class B
B cB;
and then hoping x will be the same value in both. Won''t happen unless you use a static variable. If you''re using a struct, you could make the member a pointer to the struct and make that pointer static. Then the pointer would be pointing to the same struct for all of the derived classes unless you override it.
Mark Fassett
Laughing Dragon Entertainment
http://www.laughing-dragon.com
class A
{
protected:
int x;
};
class B : public A
{
}
and then creating an instance of class A
A cA;
and then creating an instance of class B
B cB;
and then hoping x will be the same value in both. Won''t happen unless you use a static variable. If you''re using a struct, you could make the member a pointer to the struct and make that pointer static. Then the pointer would be pointing to the same struct for all of the derived classes unless you override it.
Mark Fassett
Laughing Dragon Entertainment
http://www.laughing-dragon.com
I think you might be a bit confused, could you paste your code so that I can see what you are tring to accomplish.
When you inherit , your inherited class has all the attributes and methods of it's parent class. ( Not in all circumstances, but I don't want to discuss that here. )
So, A.m_iX does in fact equal B.m_iX.
Example:
Result from above:
TestA Constructed
TestB Constructed
From Test A : 10
From Test B : 15
TestB Destroyed
TestA Destroyed
Press any key to continue
Edited by - AlekM on July 12, 2000 12:14:12 AM
When you inherit , your inherited class has all the attributes and methods of it's parent class. ( Not in all circumstances, but I don't want to discuss that here. )
So, A.m_iX does in fact equal B.m_iX.
Example:
#include <iostream>using namespace std;class TestA{protected: int m_iTestA;public: TestA() { m_iTestA = 0; cout << "TestA Constructed" << endl; } TestA( int iTestA ):m_iTestA( iTestA ){ cout << "TestA Constructed" << endl; } virtual ~TestA(){ cout << "TestA Destroyed" << endl; }};class TestB : public TestA{ int m_iTestB;public: TestB(){ m_iTestB = 1; cout << "TestB Constructed" << endl; } TestB( int iTestA, int iTestB ):TestA(iTestA),m_iTestB(iTestB){ cout << "TestB Constructed" << endl; } virtual ~TestB(){ cout << "TestB Destroyed" << endl; } void Report() { cout << "From Test A : " << m_iTestA << endl << "From Test B : " << m_iTestB << endl; }};int main( int argc, char* argv[] ){ TestB* ptrB = new TestB( 10, 15 ); ptrB->Report(); delete ptrB; return 0;}
Result from above:
TestA Constructed
TestB Constructed
From Test A : 10
From Test B : 15
TestB Destroyed
TestA Destroyed
Press any key to continue
Edited by - AlekM on July 12, 2000 12:14:12 AM
LOL-Yes I''m a Hard Head--1st:
From Test A : 10
From Test B : 15
I need Test B to = 10 too. And yes static will do this, but this keeps happening when I compile:
class C_DDraw {
public:
static int huh;
}
Tetris.obj : error LNK2001: unresolved external symbol "public: static int C_DDraw::huh" (?huh@C_DDraw@@2HA)
Debug/Tetris.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Arghhh!
From Test A : 10
From Test B : 15
I need Test B to = 10 too. And yes static will do this, but this keeps happening when I compile:
class C_DDraw {
public:
static int huh;
}
Tetris.obj : error LNK2001: unresolved external symbol "public: static int C_DDraw::huh" (?huh@C_DDraw@@2HA)
Debug/Tetris.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Arghhh!
OK-Here's the code
Edited by - Chipcrap on July 12, 2000 12:38:30 AM
#include <ddraw.h>#include <mmsystem.h>class C_DDraw {public: static int huh; C_DDraw(); ~C_DDraw(); int Init(int w, int h, int c, HWND main_window_handle); void Destroy(); void RenderInt(int FPS); void Flipper(); void RenderGraphic(); int GetGraphic(char *file, HWND hwnd); protected: static LPDIRECTDRAWSURFACE7 *junk; LPDIRECTDRAWSURFACE7 surface; LPDIRECTDRAW7 lpdd;// dd object LPDIRECTDRAWSURFACE7 lpddsback; // dd back surface LPDIRECTDRAWSURFACE7 lpddsprimary; // dd primary surface DDSURFACEDESC2 ddsd; // a direct draw surface description struct DDSCAPS2 ddscaps; // a direct draw surface capabilities struct void Error(HWND hwnd, char *ErrorMsg); }; int C_DDraw::Init(int w, int h, int c, HWND hwnd){ huh=3;// create object and test for errorif (DirectDrawCreateEx(NULL,(VOID**)&lpdd, IID_IDirectDraw7,NULL)!=DD_OK) { Error(hwnd, "DirectDrawCreate"); return(0); }// set cooperation level to windowed mode normalif (lpdd->SetCooperativeLevel(hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!=DD_OK) { Error(hwnd, "SetCooperationLevel"); return(0); }// set the display modeif (lpdd->SetDisplayMode(w,h,c,0,0)!=DD_OK) { Error(hwnd, "SetDisplayMode"); return(0); }// Create the primary surfaceZeroMemory(&ddsd,sizeof(ddsd));ddsd.dwSize = sizeof(ddsd);ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;ddsd.dwBackBufferCount=1;if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK) { Error(hwnd, "CreateSurface"); return(0); }ZeroMemory(&ddscaps, sizeof(ddscaps));ddscaps.dwCaps=DDSCAPS_BACKBUFFER;if (lpddsprimary->GetAttachedSurface(&ddscaps, &lpddsback)!=DD_OK) { Error(hwnd, "GetAttachedSurface"); return(0); }return(1);}class C_StaticSprite : protected C_DDraw{public: int GetGraphic(char *file); void RenderGraphic();protected: LPDIRECTDRAWSURFACE7 surface; DDSURFACEDESC2 ddsd;};int C_StaticSprite::GetGraphic(char *file){ HDC hdc; HBITMAP hBitmap; // Load the bitmap hBitmap = (HBITMAP)LoadImage(NULL, file, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE); if (!hBitmap) { return NULL; } // Get bitmap dimensions BITMAP bitmap; GetObject(hBitmap, sizeof(BITMAP), &bitmap); // Create surface DDSURFACEDESC2 ddsd; // Set up the stucture with the correct details ZeroMemory(&ddsd,sizeof(ddsd)); ddsd.dwSize = sizeof(DDSURFACEDESC2); ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth = bitmap.bmWidth; ddsd.dwHeight = bitmap.bmHeight; // Attempt to create surface if ((C_DDraw::lpdd->CreateSurface(&ddsd, &surface, NULL)) != DD_OK) { return NULL; } // Attempt to create surface if (surface == NULL) { // Release the bitmap and return failure to caller DeleteObject(hBitmap); return NULL; } else { // Get a device context for our surface surface->GetDC(&hdc); // Create a compatible device context HDC bitmap_dc = CreateCompatibleDC(hdc); // Blit the bitmap to the surface SelectObject(bitmap_dc, hBitmap); BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, bitmap_dc, 0, 0, SRCCOPY); // Release the DCs surface->ReleaseDC(hdc); DeleteDC(bitmap_dc); } // Clear bitmap DeleteObject(hBitmap); // Return pointer to caller return 1;}void C_StaticSprite::RenderGraphic(){ //THIS IS THE PROBLEM //lpddsback needs to be the original from //C_DDraw or it will not work lpddsback->Blt(NULL, surface, NULL, DDBLT_WAIT, NULL);}
Edited by - Chipcrap on July 12, 2000 12:38:30 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement