Void function (What will this program print out?

This is a discussion on Void function (What will this program print out? within the C++ Programming forums, part of the General Programming Boards category; Actually, my book says a variable in an inner scope masks one in an outer scope. It uses hiding in ...

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Actually, my book says a variable in an inner scope masks one in an outer scope.
    It uses hiding in the context of inheritance. I've never heard shadowing before.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Shadowing, as in shadowing a parameter?
    Code:
    void hippo(int var) {
        int var;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    #include <iostream>
    
    int x = 20;
    
    int main()
    {
        int x = 50;
    
        std::cout << "Local x: " << x << std::endl << "Global x: " << ::x << std::endl;
    
        return 0;
    }
    Output:
    Code:
    Local x: 50
    Global x: 20
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #19
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    The answer needs to be x=5 and y=21

    The reasoning is very simple - scope of the variables. x takes a valude of 5 globally and hence does not get modified inside main() . However main() has local variable x and that is the one that is printed by the print statement.

    Y on the other hand is also a global variable and gets modified along with "global x" and only "global y" gets printed out from the main. If there was another "local y" declared then this "local y" would get printed instead.

  5. #20
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by 7stud
    Actually, my book says a variable in an inner scope masks one in an outer scope.
    It uses hiding in the context of inheritance. I've never heard shadowing before.
    Oh god, masking... will the similar terms never end.

    In this code fragment the scope operator is used to address a global variable instead of the local variable with the same name. In C++ the scope operator is used extensively, but it is seldomly used to reach a global variable shadowed by an identically named local variable. Its main purpose will be described in chapter 6.
    http://www.icce.rug.nl/documents/cpl...lusplus03.html
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

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

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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