Thread: Problems defining classes

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    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,366
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 01:26 AM
  3. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01: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