Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Easy Classes Question

Author
Message
Third Dimension Artist
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location:
Posted: 18th Mar 2011 05:02
Hello,

I learned all my C++ from the recommended book on the GDK section of the TGC site. I have set up some classes that I want to use in my game. The classes work fine, but here's my problem:

If I say:

MyClass myObject; // Constructed my object here

I am allowed to say:

myObject.function( ); // Just called a member function

This works fine, but only if the member function is called in the same scope as the construction. What if I wanted to call an object's member function within a function?

For example, if I have a function called setup( ) in which I construct all my objects, in the main loop I can no longer call myObject.function( ). Why is this and how can I make this work?

I'm happy to rephrase, elaborate, or answer any clarifying questions.

Thanks!
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 18th Mar 2011 05:58 Edited at: 18th Mar 2011 06:03
You do not need to call it as myObject.function() from within your class code. Just use function() as normal.

This will however mean that if there is another function with the same name then the class function name will override it. You will very rarely come across that though...



Warning! May contain Nuts!
Third Dimension Artist
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location:
Posted: 18th Mar 2011 13:23
Sorry, I wasn't very clear. I'll rephrase.

Why does the compiler give an error for this code? I know that the header file is OK



The compiler says that is does not recognize what myObject is. How can I keep the setup function yet have the compiler recognize that I have constructed myObject?
Juggalo Memnoch
16
Years of Service
User Offline
Joined: 22nd Apr 2008
Location:
Posted: 18th Mar 2011 15:09
I'm still new to classes myself but I didn't think you could define classes inside functions... Have you tried setting up the class before the setup function?

If I'm wrong I would love to know

Ps. If that sounded sarcastic it wasn't meant to be lol
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 18th Mar 2011 15:32 Edited at: 18th Mar 2011 15:37
the MyClass object is defined in the setup() function scope, and thus, only setup() body can see it, the3re are billions of workarounds depending on the code structure, one possibility would be to pass it as a parameter

*note: a pointer is passed, because parameters only contain a copy of the object, you could also pass a reference, or have another solution, it's up to you

or define the object in global scope


Quote: "I'm still new to classes myself but I didn't think you could define classes inside functions... Have you tried setting up the class before the setup function?

If I'm wrong I would love to know"

no that's wrong, you can create a class instance/object inside functions or anywhere you want, as long as it knows that the class is defined (that means the class must be in a viewable scope for the function), you could even define the whole class body inside a function, like


Juggalo Memnoch
16
Years of Service
User Offline
Joined: 22nd Apr 2008
Location:
Posted: 18th Mar 2011 15:54
Ahh ok.. Thank you very much Hassan
Third Dimension Artist
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location:
Posted: 18th Mar 2011 23:06
Yes thank you very much. I'll give this a try.

Login to post a reply

Server time is: 2024-11-19 22:08:28
Your offset time is: 2024-11-19 22:08:28