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.

AppGameKit Classic Chat / [Tier 2] how do I represent a class object a a variable?

Author
Message
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 22nd Feb 2013 07:17
Hello,

I have been teaching myself C/C++ for about 6 months and am writing my first class finally.

The question I'm stumbling on at the moment is I am going through a for loop and I want to check each class object as I go through the for loop.

So sample class creation here:



Okay so great created a class... now I create 2 objects



So in the instance above in my loop I have created two objects: 1, and 2 (if this is wrong please correct me)

Finally the crux of the question .. so now I want to go through another loop and check the value in the timer for each object.

I know I can do 1.timer 2.timer .. but I might actually have a hundred of these so I want to do it in a loop.

But when I do this I get an error from visual studio


The error is : Error: expression must have class type.

I have tried writing it other ways ... x.timer being the most obvious also does not work.

So .. any C++ coders out there have some clues for me how to approach this better?

Thank you in advance!

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 22nd Feb 2013 07:49 Edited at: 22nd Feb 2013 07:51
Quote: "Okay so great created a class... now I create 2 objects"

I'll admit, I've never seen code like that before. There is one problem with creating objects like that however, as soon as that for loop is finished, those objects are deleted (popped of the call stack).

A variable's lifespan is that of which the scope it's created - i.e between the two { } it's created in.

So doing something like this won't work:



A current hurdle with your class setup is that you can't directly access your variables because they are private. Typical OOP practice (but comes down to personal preference) is to create getter and setter methods for each variable. So your class should look something like:



The best way to store a bunch of object is in an array.

MyClass objects[10]; // default constructor called for each

Then run a for loop to set the timer:
for(int i = 0; i < 10; i++) objects[ i ].set_timer(agk::Timer());

Hope that helps!

erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 22nd Feb 2013 16:08
Hi Hodgey

thanks for the reply.

I had considered the array approach but I will not know ahead of time how many objects of this class I will need so it was either a huge array or some other approach..

I was definitely worried about the objects dissapearing off the stack but wasn't sure that's how objects worked especially if I end up making an explicit deconsructor or not so was seeing if I could pull it off.

I guess I need to re-design a little bit at this point.

Thank you

Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 22nd Feb 2013 18:03
Quote: "I had considered the array approach but I will not know ahead of time how many objects of this class I will need so it was either a huge array or some other approach.."

You could use a vector or a list instead of a straight array for variable-sized containers.

george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 23rd Feb 2013 21:28
@Dar13: I agree with you but I don't think it's a good advice for a 6 months C++ programmer to dive into C++ vectors
erebusman
12
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 25th Feb 2013 22:14
I ended up scratching the class and went with a structure instead. I had a string/char[256] array in my class and could not get it to do ANYTHING at all ..

So I'll have to brush up on classes and take another whack at 'my first class' again some day soon Thanks for the tips though folks

The Daddy
15
Years of Service
User Offline
Joined: 13th Jan 2009
Location: Essex
Posted: 3rd Mar 2013 21:38
Linked list of classes is the way forward!

I know this is quite straight forward in object pascal but is more complicated in C++.

This is one of the main reasons I jumped ship to Object Pascal (that and the MANY issues with getting templates to compile).., thanks to Erik's hard work my first object Pascal AppGameKit program compiled and ran first time! That alone says a lot for me!

If you do not fancy vectors or linked lists then you are left with arrays!

Constantly seeking!
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Mar 2013 23:32 Edited at: 3rd Mar 2013 23:34
I agree with The Daddy.

There are circumstances where arrays are useful - like ring buffers. These are vital for real-time audio processing and many other things. For example, in our multi-user language lab network messages can arrive at arbitrary times, and it's important to get the message out of the network interface as fast as possible. We use a ring buffer with read and write pointers, and two handler procedures which look (simplified) like this:



In this way, the messages are saved without blocking the network event handlers, which want you to exit very fast.

This kind of technique can be easily coded in Tier 1 as well as other languages, with varieties of data.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL

Login to post a reply

Server time is: 2024-05-06 12:06:17
Your offset time is: 2024-05-06 12:06:17