Thread: A very strange thing

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Question A very strange thing

    Hi everyone! I need help with my current game. It is may first game, so I have a lot of new ideas. But I have a problem dealing with event listeners. The listeners need to know a lot of variables that exists inside main. But they are a different function, of course. My question is: How can I acess the main variables using another method, without having to use they like parameters.

    I could use globals, but when I talk variables I mean a lot of them.
    The only thing that "looks like"(damn!! sorry for the bad english) is the friend key word. I´m so sad...
    One interesting thing: everyone knows that when inline function are used, the code is, actually, coppied to the position of the call, but I think that this is useless.

    An example of the horrible thing I´m trying to do;

    int main(){
    int an_int;

    //do something with an_int

    f();

    return 0;

    }

    void f(){

    //do more things with an_int

    }

    Thanks for any help!!!
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    If it is just a large group of varibles that you need to pass into functions you could just make a struct with all the variables in it, that way you would only have to pass the struct in every time. If you pass it in by reference you will not allocate any new memory but just simply use the memory that was allocated inside main.

    for example:

    Code:
    struct vars {
          int an_int;
    }
    
    void f(vars &);
    
    int main()
    {
           vars game;
           game.an_int = 0;
    
          // do something with game.an_int;
    
           f(game);
    
           return 0;
    }
    
    void f(vars & game)
    {
           // do more with game.an_int;
    }
    you can have as many variables as you want inside the vars struct and all you have to pass around is game. Also no memory will be lost as it will pass around the address of game instead of copying all the values into new memory when you pass game around. The only bad thing is that you have to put "game." infront of every variable.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    hello and welcome, firstly you should read the top two posts on this board, and use code tags.

    As to your question, try using a class...you can use the class object in main as well as a function referenced in the class.

    hope this helps,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    I'm not really understanding your objection to passing an_int as an argument to f()...seems to me like that's the only way to get it done other than making an_int global(urg). If there are other game data related to an_int...maybe you could make an object where an_int is a data member, and then make the listener a member function.

    One interesting thing: everyone knows that when inline function are used, the code is, actually, coppied to the position of the call, but I think that this is useless.
    Normal functions take time to call. Inline functions take very little, if any, time to call. So, if you have a function that is small and is called often, you would make your program faster by making that function inline to reduce the call time.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    THank YoU AlL!

    Thank you every one!
    That was the idea that I had this morning.
    About saying that inline is useless, it is just for my problem...

    I´ll see what I can do, but again thanks to all of you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange struct syntax; type name:number
    By OnionKnight in forum C Programming
    Replies: 1
    Last Post: 11-19-2006, 08:23 PM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  3. "new" is strange
    By The Wazaa in forum C++ Programming
    Replies: 26
    Last Post: 03-06-2006, 12:32 PM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. Very strange linker error
    By hajohoff in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2005, 07:27 AM