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

  • Home
  • SEARCH
  • 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 8897485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:21:27+00:00 2026-06-15T00:21:27+00:00

Observe the following: #include <iostream> #include <string> #include <cstdlib> int main(){ static std::string foo

  • 0

Observe the following:

#include <iostream>
#include <string>
#include <cstdlib>

int main(){

    static std::string foo = "inside main";

    struct Bar{
        Bar(){
            std::cout << "I can see " << foo << '\n';
        }
    };

    Bar b;

    return EXIT_SUCCESS;
}

The output of this program is: “I can see inside main”.

Why can the class constructor look outside the class definition and find foo?

It only works, if foo is static, inside the same function as the class definition, and comes before the class definition.


Help convince me that it’s not violating the rules of scope. Why is it possible? What are the advantages and pitfalls of such an implementation?

  • 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-06-15T00:21:28+00:00Added an answer on June 15, 2026 at 12:21 am

    Because struct Bar is inside main()’s namespace and foo is static. The standard says:

    A class can be defined within a function definition; such a class is
    called a local class. The name of a local class is local to its
    enclosing scope. The local class is in the scope of the enclosing
    scope, and has the same access to names outside the function as does
    the enclosing function. Declarations in a local class can use only
    type names, static variables, extern variables and functions, and
    enumerators from the enclosing scope.

    So your code doesn’t violate the standard.

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

Sidebar

Related Questions

Consider the following code: #include<stdio.h> #define k 0.7 #define p 0.5 int main() {
Observe the following code: private List<List<Place>> m_grid = constructGrid(10000); private static List<List<Place>> constructGrid(int size)
Observe the following piece of an msbuild script: <ItemGroup> <R Include=-Microsoft.Design#CA1000 /> <R Include=-Microsoft.Design#CA1002
Why does this happen? Please observe the following code: static class StringExtension { public
Consider the following code: #include <stdlib.h> #ifndef TRY #define TRY struct #endif TRY testme
Consider the following program, which is obviously buggy: #include <cstdio> double test(int n) {
Observe the following table model: CREATE TABLE [site].[Permissions] ( [ID] INT REFERENCES [site].[Accounts]( [ID]
Observe the following program written in Java (complete runnable version follows, but the important
Observe the following Python script, Script.py: import subprocess src_directory = 'Z:\z_7z\Some_Directory' zip_file_name = 'Test.7z'
I observe the following strange behaviour in Vim 7.3: When I'm below a fold,

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.