Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:06:01+00:00 2026-05-23T12:06:01+00:00

Please see the C++ code fragment below: #include ….. Class1 class1; Class2 class2; …

  • 0

Please see the C++ code fragment below:

#include .....

Class1 class1;
Class2 class2;
...

void Class3::foo() {
    ...
}

What are the variables: class1 and class2? Are they global variables? Static variables? What actually are these? In C++ OO programming, is it good practice to use these because any member function of any class in the file can access them?

Sorry for the beginner’s question.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:06:02+00:00Added an answer on May 23, 2026 at 12:06 pm

    Yes class1 & class2 are global variables.

    What are global variables?
    Variables declared outside of a block are called global variables. Global variables have program scope, which means they can be accessed everywhere in the program, and they are only destroyed when the program ends.

    Because global variables have program scope, they can be used across multiple files. In order to use a global variable that has been declared in another file, you have to use a forward declaration or a header file, along with the extern keyword. Extern tells the compiler that you are not declaring a new variable, but instead referring to a variable declared elsewhere.

    In C++ OO programming, is it good practice to use these because any member function of any class in the file can access them?

    Typically, people use global variables because:

    • They don’t understand C++ variable passing mechanics, or they’re being lazy.
    • To hold data that needs to be used by the entire program (eg. configuration settings).
    • To pass data between code that doesn’t have a caller/callee relationship (eg. multi-threaded programs)

    But Global variables are evil!!
    Why?
    For the simple reason that they increase the complexity of a program by manyfolds.
    It is difficult to keep track of a global variable getting modified, simply because it can be modified anywhere across any of the multiple files.

    In multithreaded programs, multiple threads can race to acquire these global variables, So these global’ s should always be protected through some sort of an synchronization mechanism. Typically, it is hard to understand and write such mechanism unless you understand the entire system.

    Since you asked,
    What are Static variables?
    static variables are variables which will be qualified by the keyword static.

    How are static variables different from global variables?
    An important differentiating point to be considered:

    Scope:
    Scope of the object is whether the object is visible(known by its name) at this position where it is being accessed..

    Static variables are local to the block in which they are defined, while global variables are accessible across any file across the program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see the code fragment below, specifically the first 'else if' statement. I want
Please see code. return from org in context.Organizations select new { PlaceId = org.OrganizationId,
Please see code at http://cyclestreets.darkgreener.com/location/ - if necessary, go to a London, UK location
(please excuse that I didn't use aliases). I would like my query output to
Is there a way I could limit the tds to 4 or any number
I am having a strange QNX problem here in Flex (Adobe AIR), I have
I have to pass a parameter into a condition to select rows, if I
I have an array with this type of content a/a/a/test134.html a/a/a/test223.html a/b/b/test37.html a/b/test41.html a/b/test44.html
HI, I have a list of coursesTaken with dates (returned by a nested class).
I have a collapsible div implemented in js, the function of which is show/hide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.