Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Void

How to do GOSUB with goto in C++??

This topic is 6932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Juz wondering, if anyone knows of a simple way to do a GOSUB routine easily in C++ Can''t really think of a simple way.. ..

Share this post


Link to post
Share on other sites
Advertisement
Well the point is that you don''t

Gosub/Goto are very very very very very very very very very very very bad.
Unless you really know what you are doing. And even then.

Procedures and functions are what you should be using.
It''s much harder to write spaghetti code without goto''s.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
Well not really bad..

I use goto''s for error handling and I want to use the gosub as something similar to the /bfinally/b call in exceptions, so no big problems there.

Share this post


Link to post
Share on other sites
What''s the exact context you''d be using it in? I can''t really imagine a way that you''d need to "goto" in those cases.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
Here''s a blurb from MSDN:

Using Labels with the goto Statement

The appearance of an identifier label in the source program declares a label. Only a goto statement can transfer control to an identifier label. The following code fragment illustrates use of the goto statement and an identifier label to escape a tightly nested loop:


for( p = 0; p < NUM_PATHS; ++p )
{
NumFiles = FillArray( pFileArray, pszFNames )
for( i = 0; i < NumFiles; ++i )
{
if( (pFileArray = fopen( pszFNames[i], "r" )) == NULL )
goto FileOpenError;
// Process the files that were opened.
}
}

FileOpenError:
cerr << "Fatal file open error. Processing interrupted.\n" );


In the preceding example, the goto statement transfers control directly to the statement that prints an error message if an unknown file-open error occurs.

A label cannot appear by itself but must always be attached to a statement. If a label is needed by itself, place a null statement after the label.

The label has function scope and cannot be redeclared within the function. However, the same name can be used as a label in different functions.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
I use them for cleaning up pointers



void Function
{

// hr failure from DirectX calls
// goto FAILURE;

// failure from C libs
// goto C_ERROR;

FAILURE:

// clean up memory and pointers
// use hr translation and display error

C_ERROR:

// clean up memory and pointers - duplicated here
// display error from system

}


The cleaning up portion has to be duplicated. If there was a gosub call, I could make both labels call them before calling the appropriate messagebox display.

Share this post


Link to post
Share on other sites
A "gosub" call is nothing more than a procedure call in basic though, if I''m not mistaken. Why don''t you just make an inline function?


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
Inline functions.. hmm.. but it wouldn''t have access to the local variables and pointers.

I was thinking of something like



Func
{


FAILURE:

// call cleanup - gosub
// return and display messagebox using hr

C_ERROR:

// call cleanup - gosub
// return and display error from system

CLEANUP:

// only cleanup here
// and return to caller

}

Share this post


Link to post
Share on other sites
If you need to access local variables and pointers, perhaps you should make them member variables instead, or pass them as parameters to the cleanup function..

#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!