Advertisement

Automatic initialisation of member variables

Started by October 17, 2000 09:45 AM
3 comments, last by Danack 24 years, 2 months ago
Does anyone know of a way of initialising member variables of derived and virtual classes ?
    

class Joypad : public InputDevice
{
private:

	LPDIRECTINPUTDEVICE2		pJoystick;      
	DIJOYSTATE			m_JoystickState;

public:
.
.
.
.
}
[/source]

If the class Joypad isn''t derived class you could just do this, in it''s constructor 

[source]

Joypad::Joypad( void )
{
	ZeroMemory( this, sizeof( Joypad ) );
}

    
but obviously you can''t do this because it will overwrite the variables in InputDevice or the vtable if InputDevice is a virtual class. cheers dan Game production: Good, quick, cheap: Choose two.
Game production:Good, quick, cheap: Choose two.
Dan,

The usual way is the simple way. In your constructor, just set each variable one at a time.

        MyClass::MyClass(){  MyPointer = NULL;  MyInt = 0;  memset(MyArray,sizeof(MyArray),0);}        


Micah

Edited by - MicahJon on October 17, 2000 11:35:59 AM
Advertisement
Yeah but I''m lazy and forgetful.....which is why I didn''t want to do it by hand.

You can do it automatically by making your own new function....

    #define	WIPECODE	( 0xABBAABBA )inline void * __cdecl operator new( unsigned int size ){	char				*ptr;	unsigned int		nIndex;	unsigned int		*pInt;		ptr = (char *)malloc(size);	for ( nIndex=0 ; nIndex< size-3 ; nIndex += 4 )	{		pInt = ( unsigned int* )( ptr + nIndex );		*( pInt ) = WIPECODE;	}	return(ptr);};void CheckInitialisation( void *pNewObject, int nSize ){	int		nIndex;	int		nValue;		char	*pObject = (char*)pNewObject;	for( nIndex=0 ; nIndex<nSize-3 ;nIndex += 4 )	{		nValue = *( (int*)( pObject+nIndex ) );		if ( nValue == WIPECODE )		{			dprintf( "Warning: Uninitialsed variable at this + 0x%x = 0x%x\n", nIndex, pObject + nIndex );		}	}}    


Obviously the inline new function has to be included as the first thing in all you source files, and then at the end of each of your constuctors, call:

CheckInitialisation( this, sizeof( ''class'' ) );

and it''ll give you a lovely little error message

cheers dan




Game production:
Good, quick, cheap: Choose two.
Game production:Good, quick, cheap: Choose two.
Dan,

I''m just as forgetful as the next programmer. When I define an object, I keep the variables and function prototypes seperate. However, I usually put two small inline functions with the variables called CopyFrom and Initialize. Initialize sets each of the variables to some value and is called from the real constructor, CopyFrom is used with the copy constructor and assignment operator to copy each variable from another object. When I add members, the functions are right there so I don''t forget to update them with the new members. (Well, that''s a lie, but I forget less often than normal )

Micah

The best way to initialize stack-based data members is using this syntax:


class point
{
public:
inline point();

int x;
int y;
};

inline point::point()
: x(0), y(0)
{
}



Note that most pointers are just variables on the stack, so the same syntax works for them, too.


class linked_list
{
public:
inline linked_list();

protected:
class node { /* fill this in */ };

node* head;
node* tail;
node* current;
};

inline linked_list::linked_list()
: head(NULL), tail(NULL), current(NULL)
{
}



You should not overload the new operator to perform that kind of initialization - just use the methods already provided in the language.


- null_pointer
Sabre Multimedia

This topic is closed to new replies.

Advertisement