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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:10:32+00:00 2026-05-17T00:10:32+00:00

#include<iostream> using namespace std; class A { private: const int a=9; public: void display()

  • 0
#include<iostream>
using namespace std;
class A
{
        private:
                const  int a=9;
        public:
                void display()
                {
                    cout<<a;
                }
};
int main()
{
        A a;
        a.display();
        return 0;
}

Why does initialization const int a=9 is not permitted.
But where as if i write constant static int a=9 compiler does not show any error. What is the meaning of writing const static int a=9? when should i write like this ?
~

  • 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-17T00:10:33+00:00Added an answer on May 17, 2026 at 12:10 am

    Use constructor initializer list to initialize non-static constant members.

    ISO C++03 says the following things about static data members.

    [class.static.data]

    9.4.2 Static data members

    1 A static data member is not part of the subobjects of a class. There is only one copy of a static data member shared by all the objects of the class.`

    2 The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a staticdata member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. `

    If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a name-space scope if it is used in the program and the namespace scope definition shall not contain an initializer*.

    class A
    {
            private:
    
                    const  int a=9; //incorrect 
                    static const int b = 10; //declaration (correct)
                    static const double c = 1.3 //incorrect (Only const-static int members can be initialized like that)
    
    
            public:
    
                    A(): a(9){} 
    };
    
    const int A::b; //definition of const-static int member
    

    You can take the address of a static member if (and only if) it has an out-of-class definition:

    class AE {
        // ...
    public:
        static const int c6 = 7;
        static const int c7 = 31;
    };
    
    const int AE::c7;   // definition
    
    int f()
    {
        const int* p1 = &AE::c6;    // error: c6 not an lvalue
        const int* p2 = &AE::c7;    // ok
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
#include <iostream> #include <boost/shared_ptr.hpp> using namespace std; class A { public: const shared_ptr<const int>
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<Something initialized. j=<<j<<endl;}
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; class A { public: A() { cout << Default
please consider following code #include <iostream> using namespace std; class Digit { private: int
#include <iostream> #include <cstring> #include <QString> using namespace std; class A { public: static
Look at this code please: #include <iostream> using namespace std; class Ratio { public:

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.