Making global inside a block

This is a discussion on Making global inside a block within the C++ Programming forums, part of the General Programming Boards category; I think he wanted to run a program without having a long list of global variables that may be unnecessary. ...

  1. #16
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    I think he wanted to run a program without having a long list of global variables that may be unnecessary.

    i.e.
    Code:
    Function one()
    {
       SomeClass visible_to_all;
    }
    
    Function two()
    {
       AnotherClass visible_to_all;
    }
    A program may run 7/10 times without ever calling function two() thus the declaration of the global variable never had to happen.

    If this is your problem then why not use global pointers? Otherwise the post above this one with global declarations seems fine.

    ex: global pointers.
    Code:
    int *variable1;  // This is a global declaration.
    double *variable2;  // This is a global declaration.
    
    void functionOne()
    {
      variable1 = new int[50];
    }
    
    void functionTwo()
    {
      variable2 = new double[50];
    }
    Thus you have global variable declarations that are technically global variables, but aren't really created in memory until a desired function is called in your .lib file. Then you could just extern your pointers in the files that need access to them or include the .lib file that contains the pointer declarations. Would this work for you?

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    int *variable1; // This is a global declaration.
    Have to nitpick here: it's a definition. Variable declarations have the extern keyword. (Except in class definitions.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    OK, I changed my mind. I Made the vars filescope in the .lib. Only functions in the .lib can access those vars.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

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

Similar Threads

  1. making a wstring variable global scope
    By stanlvw in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2008, 03:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. My memory management solution
    By cboard_member in forum Game Programming
    Replies: 20
    Last Post: 08-23-2006, 10:07 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  5. definition inside block
    By PutoAmo in forum C Programming
    Replies: 13
    Last Post: 04-19-2002, 10:20 AM

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