Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualMarlboroKing

Posted 15 February 2013 - 03:08 PM

You can try what Hodgman said by something like this (I just find this a bit more clear!)

 

class MyClass
{
public:
	MyClass()
	{
		memset( &m_szName, 0, sizeof(m_szName) );
		strcat_s( m_szName, "I'm an object!" );
	}

	~MyClass()
	{
	}

public:
	char m_szName[64];
};

void DoSomethingHere()
{
	printf( "DoSomethingHere()\n" );
}

void DoSomethingElseHere( MyClass var )
{
	printf( var.m_szName );
	printf( "\n" );
}

void DoAnotherThingHere( MyClass var )
{
	memset( var.m_szName, 0, sizeof(var.m_szName) );
	strcat_s( var.m_szName, "Rewriting var.m_szName" );
	printf( var.m_szName );
	printf( "\n" );
}

int _tmain(int argc, _TCHAR* argv[])
{
	DoSomethingHere();

	{
		MyClass obj;
		DoSomethingElseHere( obj );
	}

	DoAnotherThingHere( obj );

	Sleep( 50000 );

	return 0;
}

 

 

And yes, there are certain benefits you can abuse by using code blocks like above (eg; a large command table).

Visual Studio gave me an error that obj is undefined when trying to call "DoAnotherThingHere", letting me know that "obj" would no longer exist after it has executed passed that end-codeblock.

 

Along the way, you'll understand when to allocate on the stack or the heap.


#3MarlboroKing

Posted 15 February 2013 - 03:07 PM

You can try what Hodgman said by something like this (I just find this a bit more clear!)

 

class MyClass
{
public:
	MyClass()
	{
		memset( &m_szName, 0, sizeof(m_szName) );
		strcat_s( m_szName, "I'm an object!" );
	}

	~MyClass()
	{
	}

public:
	char m_szName[64];
};

void DoSomethingHere()
{
	printf( "DoSomethingHere()\n" );
}

void DoSomethingElseHere( MyClass var )
{
	printf( var.m_szName );
	printf( "\n" );
}

void DoAnotherThingHere( MyClass var )
{
	memset( var.m_szName, 0, sizeof(var.m_szName) );
	strcat_s( var.m_szName, "Rewriting var.m_szName" );
	printf( var.m_szName );
	printf( "\n" );
}

int _tmain(int argc, _TCHAR* argv[])
{
	DoSomethingHere();

	{
		MyClass obj;
		DoSomethingElseHere( obj );
	}

	DoAnotherThingHere( obj );

	Sleep( 50000 );

	return 0;
}

 

 

And yes, there are certain benefits you can abuse by using code blocks like above (eg; a large command table).

Visual Studio gave me an error that obj is undefined, letting me know that "obj" would no longer exist after it has executed passed that end-codeblock.

 

Along the way, you'll understand when to allocate on the stack or the heap.


#2MarlboroKing

Posted 15 February 2013 - 03:06 PM

You can try what Hodgman said by something like this (I just find this a bit more clear!)

 

class MyClass
{
public:
	MyClass()
	{
		memset( &m_szName, 0, sizeof(m_szName) );
		strcat_s( m_szName, "I'm an object!" );
	}

	~MyClass()
	{
	}

public:
	char m_szName[64];
};

void DoSomethingHere()
{
	printf( "DoSomethingHere()\n" );
}

void DoSomethingElseHere( MyClass var )
{
	printf( var.m_szName );
	printf( "\n" );
}

void DoAnotherThingHere( MyClass var )
{
	memset( var.m_szName, 0, sizeof(var.m_szName) );
	strcat_s( var.m_szName, "Rewriting var.m_szName" );
	printf( var.m_szName );
	printf( "\n" );
}

int _tmain(int argc, _TCHAR* argv[])
{
	DoSomethingHere();

	{
		MyClass obj;
		DoSomethingElseHere( obj );
	}

//	DoAnotherThingHere( obj );

	Sleep( 50000 );

	return 0;
}

 

 

And yes, there are certain benefits you can abuse by using code blocks like above (eg; a large command table).

Visual Studio gave me an error that obj is undefined, letting me know that "obj" would no longer exist after it has executed passed that end-codeblock.

 

Along the way, you'll understand when to allocate on the stack or the heap.


#1MarlboroKing

Posted 15 February 2013 - 03:06 PM

You can try what Hodgman said by something like this (I just find this a bit more clear!)


class MyClass
{
public:
    MyClass()
    {
        memset( &m_szName, 0, sizeof(m_szName) );
        strcat_s( m_szName, "I'm an object!" );
    }
 
    ~MyClass()
    {
    }
 
public:
    char m_szName[64];
};
 
void DoSomethingHere()
{
    printf( "DoSomethingHere()\n" );
}
 
void DoSomethingElseHere( MyClass var )
{
    printf( var.m_szName );
    printf( "\n" );
}
 
void DoAnotherThingHere( MyClass var )
{
    memset( var.m_szName, 0, sizeof(var.m_szName) );
    strcat_s( var.m_szName, "Rewriting var.m_szName" );
    printf( var.m_szName );
    printf( "\n" );
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    DoSomethingHere();
 
    {
        MyClass obj;
        DoSomethingElseHere( obj );
    }
 
    DoAnotherThingHere( obj );
 
    return 0;
}

 

And yes, there are certain benefits you can abuse by using code blocks like above (eg; a large command table).

Visual Studio gave me an error that obj is undefined, letting me know that "obj" would no longer exist after it has executed passed that end-codeblock.

 

Along the way, you'll understand when to allocate on the stack or the heap.


PARTNERS