Why is it not possible to define a static (public) member of a class in the main() function?

Aditya S

Why is it not possible to define a static member of a class in the main() function in C++? Consider this code:

#include <iostream>
using namespace std;
class A
{
    public:
    int a;
    static int b;
    A()
    {
        // int A::b = 1;
    }
    void fun()
    {
        // int A::b = 1;
    }
};
int main() 
{
    A objA;
    int A::b = 1;
    return 0;
}

Output:

prog.cpp: In function ‘int main()’:
prog.cpp:20:14: error: qualified-id in declaration before ‘=’ token
     int A::b = 1;
              ^

But when I am trying to do the same outside of the main function, it is working. What is the reason for this?

Dai

Why is it not possible to define a static member of a class in the main() function in C++?

C++ is a statically typed language. That is: the "shape" of all "things" (struct, class, functions, etc) must be known in-advance by the compiler (i.e. statically) - and due to historical reasons C++ requires all type-names and type-members to be declared and (excepting for member function bodies) defined ahead of time (that's what .h files are for).

If programs were able to change the "shape" of something retroactively inside of a function then that would make it impossible for the compiler to know what the "shape" of a value is elsewhere in the program (well, this is what dynamically typed languages like JavaScript do, and it's why writing an AOT compiler for those is very hard).

An alternative approach, whereby the compiler does allow function implementations to define types is known as Hindley–Milner Type Inference, however C++ does not support it.

I note that the unsolvable problem with naïve implementations of Hindley-Milner is that it's impossible for the compiler to determine if a referenced, non-declared, member should count as a new declaration - or as an error: so given your program - if you were to fat-finger and type int A::B = 1; instead of int A::b = 1 should the compiler give you an error or should it define B in addition to b? If it should give you an error how can it know if B or b should be the correct member?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it possible to define a virtual static member on a delphi class?

From Dev

Why aren't we allowed to access a private static member outside class without public member function?

From Dev

Error: main method not found in class please define the main method as: public static void main(String[] args)

From Dev

Why is it possible to change the visibility of a virtual member or a public base class?

From Dev

Define main function in class

From Dev

Why can't we define main function in static inner classes?

From Dev

function new or public static main

From Dev

function new or public static main

From Dev

Error: Main method not found in class inter333, please define the main method as: public static void main(String[] args)

From Dev

Define the main method as: public static void main(String[] args)

From Dev

Why do we need to define a static variable of a class, unlike the other member of a class?

From Dev

Why can't this public member function call decltype on a private struct member declared inside the class?

From Dev

Why main() is declared public and static in java

From Dev

Static Template function access static class member

From Dev

Is it possible to define an anonymous member function in C++

From Dev

Class member behave differently when define as static or non static

From Dev

Why can a static member function only be declared static inside the class definition and not also in its own definition?

From Dev

Define function signature for a class member in Dart

From Dev

Define member variables of class inside function

From Dev

How to define a member function outside class in python?

From Dev

Initialisation of static class member. Why constexpr?

From Dev

Is 'println()' a static member function of PrintStream class or instance member function?

From Dev

Declare a public "static" function inside a JavaScript class

From Dev

Is it possible to initialize a static member variable after running the main?

From Dev

Is it possible to initialize a static member variable after running the main?

From Java

How to access a public static final member in a Java class from Scala?

From Dev

How to access a public static final member in a Java class from Scala?

From Dev

std::bind a static member function inside the class

From Dev

Calling and initializing static member function of class

Related Related

  1. 1

    Is it possible to define a virtual static member on a delphi class?

  2. 2

    Why aren't we allowed to access a private static member outside class without public member function?

  3. 3

    Error: main method not found in class please define the main method as: public static void main(String[] args)

  4. 4

    Why is it possible to change the visibility of a virtual member or a public base class?

  5. 5

    Define main function in class

  6. 6

    Why can't we define main function in static inner classes?

  7. 7

    function new or public static main

  8. 8

    function new or public static main

  9. 9

    Error: Main method not found in class inter333, please define the main method as: public static void main(String[] args)

  10. 10

    Define the main method as: public static void main(String[] args)

  11. 11

    Why do we need to define a static variable of a class, unlike the other member of a class?

  12. 12

    Why can't this public member function call decltype on a private struct member declared inside the class?

  13. 13

    Why main() is declared public and static in java

  14. 14

    Static Template function access static class member

  15. 15

    Is it possible to define an anonymous member function in C++

  16. 16

    Class member behave differently when define as static or non static

  17. 17

    Why can a static member function only be declared static inside the class definition and not also in its own definition?

  18. 18

    Define function signature for a class member in Dart

  19. 19

    Define member variables of class inside function

  20. 20

    How to define a member function outside class in python?

  21. 21

    Initialisation of static class member. Why constexpr?

  22. 22

    Is 'println()' a static member function of PrintStream class or instance member function?

  23. 23

    Declare a public "static" function inside a JavaScript class

  24. 24

    Is it possible to initialize a static member variable after running the main?

  25. 25

    Is it possible to initialize a static member variable after running the main?

  26. 26

    How to access a public static final member in a Java class from Scala?

  27. 27

    How to access a public static final member in a Java class from Scala?

  28. 28

    std::bind a static member function inside the class

  29. 29

    Calling and initializing static member function of class

HotTag

Archive