Passing along function parameters

This is a discussion on Passing along function parameters within the C++ Programming forums, part of the General Programming Boards category; Hey! Just a note to start, this is cross-posted from gamedev. I've started theorizing a timer class. I will be ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Passing along function parameters

    Hey!

    Just a note to start, this is cross-posted from gamedev.

    I've started theorizing a timer class. I will be using SDL's timer functionality, and basically what I would love to do is have my program start having SDL tick away once I initialize the class, and then let the user start a new timer (not an SDL timer), and end it, allowing me to just compare how many ticks there were from SDL_GetTicks() at the beginning and the end of the user timer.

    Here's what I have:

    Code:
    #ifndef SGE_TIMER_CLASS
    #define SGE_TIMER_CLASS
    
    struct timer
    {
    	Uint32		startTime;
    	Uint32		endTime;
    }
    
    class timer
    {
    	public:
    		timer();
    		~timer();
    		
    		void start(string tstartName);
    		int end(string tendName);
    	private:
    };
    
    timer::timer()
    {
    	SDL_GetTicks();
    }
    
    timer::~timer()
    {
    	
    }
    
    void timer::start(string tstartName)
    {
    	// This is where we run into problems
    	// were actually creating a new timer with the name 'newtName'
    	// not newtName's variable set above
    	if(tstartName == NULL)
    	{
    		timer tstartName;
    		tstartName.startTime = SDL_GetTicks();
    	}
    	
    	else
    	{
    		// log error of timer's existance
    	}
    }
    
    int timer::end(string tendName)
    {
    	// Again, we have naming problems here
    	// pertaining to the variables used for the name
    	if(tendName != NULL)
    	{
    		timer tendName;
    		Uint32 startTicks = tendName.startTime;
    		tend.endTime = SDL_GetTicks() - ticks;
    	}
    	
    	else
    	{
    		// Again, log error of timer's existance
    	}
    }
    
    #endif
    Thing is, as you can see by my comments, I want to create a new type out of my structure with the name passed in the function, but it's set up to use the variables name!

    What do I need to do to use the variable passed as the name for the new structure?

    Thanks! Hopefully everybody can understand my stupidity

    FlyingIsFun1217

    PS: For those that aren't familiar with SDL, SDL_GetTicks() returns a 32 bit unsigned integer.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't create an object using a string as it's name. Instead you should create the Timer objects as needed:
    Code:
    Timer t;
    t.start();
    //...
    cout << "Elapsed time: " << t.end();
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Hmmm :/

    Well, what my ultimate goal is, is to let the person using my API say, "hey, start a timer with this ID", and then come back later (possibly after starting many such timers), and say, "oh yeah, I want to stop the one timer I started, and see how much time has passed".

    Guess what I'm shooting for is some way for the user to set the timer's ID. Can this be done in the way I described? I just remember that wxWidgets seemed to allow the user to say, "I want to create this listbox, with this ID.

    FlyingIsFun1217

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    The usual approach for a class is for it to be an object that actually services itself, providing public access methods. Like a car, it's an object which you can use an manipulate but everything that goes on to make the car drives when you press the pedestal is handled internally.

    So, you can create one timer object for each timer you want and give it a proper name. When the class constructs, the timer begins (alternatively you can also make it start the timer when you call a member function like start). And when the timer class destructs, it stops the timer (you can also make a member function that allows you to stop them).

    If this doesn't sound feasible to you, then perhaps you shouldn't be using a class for your timer. Global functions are always possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    You mean just using the class constructor and destructor to start and stop (respectively) the timer? I originally had this idea, but ran into the problem of whether or not I could have multiple SDL_GetTicks() timers going at once.

    I'll check again about the different SDL timer methods and see if I can't start multiple timers.

    Thanks!
    FlyingIsFun1217

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Try using a reference to the same sdl timer then, or do you actually need multiple sdl timers during runtime?

    The way I see it you're using a timer you've created, so as long as you always compare that timer with a reference to the single sdl timer you shouldn't have any problems.

    EDIT: Nevermind i dont think I understood the question I think
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by FlyingIsFun1217 View Post
    You mean just using the class constructor and destructor to start and stop (respectively) the timer? I originally had this idea, but ran into the problem of whether or not I could have multiple SDL_GetTicks() timers going at once.
    The idea I'm trying to give you is that each class should only hold one timer and if you need more timers, you should create more instances of the class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by Elysia View Post
    you should create more instances of the class.
    But doing that only makes other code very messy, if I'm understanding you right:

    Code:
    timer Instance1;
    timer Instance2;
    timer Instance3;
    timer Instance4;
    
    Instance1.returnTime();
    Instance2.returnTime();
    Instance3.returnTime();
    Instance4.returnTime();
    I think it would be pretty evil to make the user do this, when what I'm shooting for is something like:

    Code:
    timer tInstance;
    
    tInstance.start("gameroundTimer");
    tInstance.stop("gameroundTimer");
    I basically, REALLY want to avoid creating ton's of class instances, which brings me back to a way to let the user store an ID for a timer so that only one instance is needed, and ID's can be easily tracked by the user.

    Maybe I should look into the way that wxWidgets does it?

    Thanks again!
    FlyingIsFun1217

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The thing you don't seem to realize is that a timer and a class that lets you create and manipulate multiple timers are two different things.

    I don't see anything particularly evil in the "evil" example except you could give better names to the multiple timers (like Timer GameRoundTimer etc).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    I just think it looks a whole lot cleaner to give one instance, and let the user create many timers with different ID's.

    Before I give up on this method, there's not possible way to do this at all?

    Thanks again!
    FlyingIsFun1217

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    I think you may want something like this:

    Code:
    class TimerManager {    
        class Timer {
    	Uint32		startTime;
    	Uint32		endTime;
            Timer():startTime(0),endTime(0){}
            void start();
            int end();
        }
        map<string, Timer> m_timers;
        public:
            void start(string tName){
                //this will create a new timer if it doesn't exist.
                m_timers[tName].start();
            }
            int end(string tName){
                map<string, Timer>::iterator itr = m_timers[tstartName);
                if( itr == m_timers.end() )
                    throw TimerDoesNotExistException(); 
                int result = itr->end();
                m_timer.erase(itr);
                return result; 
            }
    }
    Last edited by King Mir; 12-29-2007 at 12:16 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I just think it looks a whole lot cleaner to give one instance, and let the user create many timers with different ID's.
    What are all these timers used for and where? Does all the code and every function and class need to know about all the timers?

    I can't see why you think the first approach is necessarily cleaner.
    Code:
    TimerManager timers;
    timers.start("timer1");
    timers.start("timer2");
    timers.stop("timer2");
    timers.stop("timer1");
    
    //vs
    Timer timer1;
    Timer timer2;
    timer2.stop();
    timer1.stop();
    Before I give up on this method, there's not possible way to do this at all?
    Of course there is. Firstly you probably shouldn't call a class that doesn't time but stores timers a timer. Then it might contain a std::map<std::string, Timer>.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Or just skip that entire class and just use a map to store timer objects.
    Each timer object should work independently and expose methods to manipulate its state without the need of a helper class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Thanks again for the help, I really do appreciate it.

    The main reason that I want to keep the amount of class instances down is because this would be for a game, something which I want to keep nice and clean.

    It looks like map could be useful, but... I've never encountered it before
    Does anybody have a nice tut that explains the concept?

    Thanks again!
    FlyingIsFun1217

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by FlyingIsFun1217 View Post
    Thanks again for the help, I really do appreciate it.

    The main reason that I want to keep the amount of class instances down is because this would be for a game, something which I want to keep nice and clean.
    That is not an excuse! You use as many instances as you need. IF implemented correctly, then it would take up the same amount of memory as a single instance of one class with multiple timers.

    It looks like map could be useful, but... I've never encountered it before
    Does anybody have a nice tut that explains the concept?

    Thanks again!
    FlyingIsFun1217
    It's called std::map. There may be a tutorial on the site.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Passing structs as parameters
    By 182 in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2006, 09:51 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Passing a function to a function
    By lend0g in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2003, 09:16 PM
  5. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 02:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21