Thread: hello, speed difference in declaring the variable, in the class, or local....

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    hello, speed difference in declaring the variable, in the class, or local....

    to the member function, so which is faster or i mean, is there any difference at all? ie,

    class aclass
    {
    int data;
    }

    or

    class aclass
    {
    function()
    }

    aclass::afunction()
    {
    int data;
    }

    so you see(considering they both have afunctin() ), i declared the variable as a member instead, while the other one, i declared it as a local variable to the function, so which is faster above? btw, don't mind about the function being local and would be destroyed etc., i just liked to know if there's a difference or what is faster etc.,

    thanks!!!

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Faster for what, speed freak?

  3. #3
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I think the first one would be faster. Because if afunction() is called multiple times it has to create the variable each time. The first way it creates it once.

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Fisrt option is faster (although I don't think you'll ever notice a difference). But if you declare the function outside the class declaration by prefixing it with the 'inline' keyword, the compiler will place it's code the same as you had it declared inside the class.

    [edit]
    err... just forget this post
    [/edit]
    Last edited by Mario; 06-01-2002 at 09:57 AM.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    there really no speed difference? ahh i see.., kewl,

    well thanks anyway guys!

  6. #6
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    um... there is no way that you can access the member field in your first class declaration, because classes members default to private (or invisible to the outside world).. you'd need to use an accessor function and declare it public in order to access the variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-08-2008, 03:45 AM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. declaring a class
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2002, 09:06 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM