Problems defining classes

This is a discussion on Problems defining classes within the C++ Programming forums, part of the General Programming Boards category; Why or when do they go out of scope?...

  1. #46
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538

    Problems defining classes

    Why or when do they go out of scope?
    Last edited by laserlight; 10-24-2007 at 01:15 PM. Reason: Remainder of post in another discussion.

  2. #47
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    This was the code for your in_ov function:
    Code:
    void autoreg::in_ov(vehicle v,owner p)
    {  
       vehicles vh;
       ovmap ovm;
       owners odata;
       long id=p.getid();
       string lic=vh.getlicense();  
    
       odata.insert(make_pair(id,p));  
       vh.insert(make_pair(lic,v));   
       ovm.insert(make_pair(id,lic));
    }
    The bold parts are the variable declarations. You are declaring the variables inside this function. It is the same as doing:
    Code:
    void print_one_to_ten()
    {
      int number = 1;
      while (number < 10)
        std::cout << number << ", ";
      std::cout << number << '\n';
    }
    The variable number in that code is local to the function, so when the function ends it goes away. If you call the function again it creates a new variable and runs the function, it doesn't use the previous one. Variables declared inside a block of code are local to that block, and they are destroyed when that block of code ends.

    In your case, you want the maps to stick around as long as your class object sticks around. For example, let's say you have a car class that has a headlight member. You want the headlights to stick around for as long as the car sticks around. If somebody turns the headlights on, then you want to remember that the headlights are on. If you create headlights local to the function and turn those on, then when the function ends the headlights will be destroyed. If somebody checks the headlights they will be off because the headlights that were on were destroyed.

    If you make the variables member variables, then they live as long as the class itself, and anything you change about them is remembered. So when you add a new vehicle to the map when it is a member variable, the vehicle will still be there later when you call another function to get or change that data.

  3. #48
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    This thread sparked off another discussion that may be worth reading: Pointers and Efficiency.
    Last edited by laserlight; 10-24-2007 at 01:15 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  2. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 12:26 AM
  3. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 12:45 PM
  4. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM

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