Hi
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.

// base.h
#pragma once

#include "global.hpp"

class CBase
{
	                    
public:

	CBase(void);   // not yet used
	~CBase(void);

	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
{
public:
	void GameLoop(I3DEngine* myEngine); 
};

// derivedClass.cpp

void CRunProg::GameLoop(I3DEngine* myEngine)
{
      Player->MoveLocalX(0.1f);
}

// main.cpp
void main()
{

	CBase* pMaster = new CBase;
	pMaster->Intialize(myEngine);
	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

Edited 5 Years Ago by RobZombie85: n/a

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.

This question has already been answered. Start a new discussion instead.