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 9315085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:17:50+00:00 2026-06-19T02:17:50+00:00

Today I found myself creating a static array of 2 ints, and because its

  • 0

Today I found myself creating a static array of 2 ints, and because its inline initalization is not allowed in C++ (not C++11), I reverted to using a static variable of type struct.

class MyWidget {
  ...
  static const struct Margin {
    const int horizontal = 1;
    const int vertical = 1;
  } margin;

};

I noticed that internal variables are used only once for all instances of struct Margin, so I decided to make them static too.

class MyWidget {
  ...
  static const struct Margin {
    static const int horizontal = 1;
    static const int vertical = 1;
  } margin;

};

What wonders me is the difference between declaring a static struct variable vs. a static struct variable with static members. AFAC static objects are allocated only once in memory, therefore Margin struct wil be allocated only once no matter if my members are static or not.

Do I miss something? Does there exist a difference or is it a mere syntactic sugar?

  • 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-19T02:17:51+00:00Added an answer on June 19, 2026 at 2:17 am

    You seem to be a bit confused about “static structs”, because in C++, there are no such things as static structs (as opposed to languages like C#, where static classes are a workaround for the fact that there are no global functions).

    What you’re doing, is creating an instance of that class, and making the instance (margin) static (and constant). So your struct is not static, you are simply defining a struct, and making a static const instance of it, belonging to MyWidget. The difference between the two given examples now, should be quite obvious.

    In the first example, you’re creating a static variable called margin, belonging to MyWidget, meaning you can access the horizontal member like so

    MyWidget::margin.horizontal
    

    Where margin is the instance you have created.

    Whereas if you made the members of the struct static, you would not be able to do that. Instead, you would have to access them like so:

    MyWidget::Margin::horizontal
    

    Where Margin is the struct. Note however, that in the second case, there is no need for the static instance margin, since it has no instance fields associated with it.

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

Sidebar

Related Questions

I was using java regexp today and found that you are not allowed to
I'm using Kohana v3 for a web project, and today I found myself writing
I was working on a project today, and found myself using Math.Max in several
Today I found the OracleCommand.InitialLOBFetchSize property ( documentation ). Because I'm using NHibernate (which
In doing a bit more programming with Firebase today, I found myself wishing for
Found myself quite confused today about this. I create a blank repository locally(hg init),
Basically I found myself today writing lots of code like below: #define VAL_1 0
today i started reading myself into the features of Zend\Form. I found a great
First hallo to everyone, first post here. Today I found myself wondering if this
Today i was reading a SO thread about array and its memory allocation. And

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.