Iam running into a problem when using classes and inheritance.
I have a base class which contains a member function whos job it is to create and intialize some variables that are shared between other classes. The other classes are inherited from the base class and the variables are accessable from all the classes however i thought that when i called the member function from the base class at the begining of my program that the variables would still be initialised when i use them in other classes but this is obviously not the case , can somebody please set me straight on how to fix this problem , thanks.

Recommended Answers

All 7 Replies

Can you post a short code example of what you're doing?

// base.h
#pragma once

#include "global.hpp"

class CBase

	CBase(void);   // not yet used

	void Intialize(I3DEngine* myEngine);

	IMesh* PlayerMesh;
	IModel* Player;

// base.cpp

void CBase::Intialize(I3DEngine* myEngine)
	// Add default folder for meshes and other media
	myEngine->AddMediaFolder( "C:\\Users\\Rob\\Documents\\TL-Engine\\Media" );
	//create the player cars
	PlayerMesh = myEngine->LoadMesh( "4x4jeep.x" );
	Player = PlayerMesh->CreateModel( 0.0f,0.0f,0.0f);

// derivedClass.h

#pragma once
#include "global.hpp"
#include "Base.h"

class CRunProg: public CBase
	void GameLoop(I3DEngine* myEngine); 

// derivedClass.cpp

void CRunProg::GameLoop(I3DEngine* myEngine)

// main.cpp
void main()

	CBase* pMaster = new CBase;
	CRunProg* run = new CRunProg;
	run->GameLoop(myEngine);                     // Runs the main game loop
	delete run,pMaster;


This is a shortened version but basically when i try move the player it gives me an unhandled exception. and in debug player is 0xcdcdcdcd

I notice that those data members aren't static, and thus aren't shared between objects. Each object you create gets its own completely separate copy of non-static data members.

that sounds exactly right , excuse my ignorance iam still learning , i have now created them as static

static IModel* Player;
but now get unresolved externals,

error LNK2001: unresolved external symbol "public: static class tle::IModel * CBase::Player" (?Player@CBase@@2PAVIModel@tle@@A)

thankyou for giving me some of your time i really appreciate it

Now you need to define them. :D Add the following to base.cpp:

IMesh* CBase::PlayerMesh = 0;
IModel* CBase::Player = 0;

For non-static data members, the constructor will define them as a part of the object. For static data members, that's not practical, so unique definitions need to be provided separately.

haha i just did a bit of research on the net on using static variables with in classes and dirived classes and came up with the exact same code you just told me to use , works perfectly now . So just to clarify what you have told me and for anyone having this problem to, Narue correct me if iam wrong but because i had declared them as static variables within the base class i also needed to define them and because i wanted derived classes to use these variables i had to define them outside of the base class to override the scope of the variables. i hope this is correct

I probably need to work on my presentation of information but thankyou very much Narue , this forum is definately worth sticking on my favs!

and because i wanted derived classes to use these variables i had to define them outside of the base class to override the scope of the variables

That has nothing to do with scope. The visibility of your static members in derived classes will be determined by the access modifier (public, protected, or private). A definition is expected outside of the class for the compiler's benefit. Rather than require compilers to implement complex logic for ensuring that only a single definition of the member is created, that requirement is delegated to the programmer, who's in a position to do it properly and without difficulty.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.