const static member initialization - inside vs outside class definition

Ramon

I know that static literal type members can be initialized in the class definition, and non-literal types can't.

class Class 
{
    static const int lt = 0; //OK
    static const std::string nlt = "hello"; //compilation error
};

However, I ran into a weird issue, where I can't use the members in STL containers if they are initialized inside the class definition, as opposed to outside.

class Class
{
public:
    static const int var = 1;
    void f();
};

void Class::f() {
    std::vector<int> vec;
    vec.push_back(var);
}

The example above results in the linker error undefined reference to Class::var If I move the initialization outside, the error goes away.

const int Class::var = 1;

What are the differences between the two initializations above? Why does one cause the error with stl containers?

R Sahu

What are the differences between the two initializations above? Why does one cause the error with stl containers?

The argument type of std::vector<int>::push_back() is int const&. Whenever a variable is used by reference or pointer, it must be defined.

A simple change to Class::f implementation will obviate the need to define Class::var.

void Class::f() {
   std::vector<int> vec;
   int v = var;
   vec.push_back(v);
}

Here, var is not used by reference. Hence, there is no need to define Class::var.

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 there a difference between defining member functions inside vs outside the class definition?

From Dev

static_assert inside/outside class definition

From Dev

In class nested static const member variable initialization Clang vs GCC which compiler is right?

From Dev

static const member variable initialization

From Dev

Initialization of a static member inside a template

From Dev

Initialize static const multidimensional array with inferred dimensions inside class definition

From Dev

Initialize static const multidimensional array with inferred dimensions inside class definition

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

initialization of static member of template class

From Dev

Static const template member initialization fails with MSVC

From Dev

{} vs. () initialization of a class member

From Dev

error: ISO C++ forbids in-class initialization of non-const static member

From Dev

outside definition of member class of an explicitly specialized member class

From Dev

C++ class static member initialization

From Dev

Forcing initialization of static data member of template class

From Dev

Static final member initialization on private class

From Dev

List initialization of static member of template class

From Dev

Forcing initialization of static data member of template class

From Dev

Whether redeclare a const static variable outside a class or not

From Dev

Difference between a const inside a proc vs outside

From Dev

Class scope constants: const vs static const

From Dev

Why we declare static variable in a class & the definition in outside of the class?

From Dev

Initialize static const member of a class, where the member is of a private type?

From Dev

std::bind a static member function inside the class

From Dev

static const variable inside a class used as template

From Dev

Initialization of static data member

From Dev

Definition of the static data member

From Dev

Initialization of a static const variable

From Dev

Builder Pattern inside vs outside class?

Related Related

  1. 1

    Is there a difference between defining member functions inside vs outside the class definition?

  2. 2

    static_assert inside/outside class definition

  3. 3

    In class nested static const member variable initialization Clang vs GCC which compiler is right?

  4. 4

    static const member variable initialization

  5. 5

    Initialization of a static member inside a template

  6. 6

    Initialize static const multidimensional array with inferred dimensions inside class definition

  7. 7

    Initialize static const multidimensional array with inferred dimensions inside class definition

  8. 8

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

  9. 9

    initialization of static member of template class

  10. 10

    Static const template member initialization fails with MSVC

  11. 11

    {} vs. () initialization of a class member

  12. 12

    error: ISO C++ forbids in-class initialization of non-const static member

  13. 13

    outside definition of member class of an explicitly specialized member class

  14. 14

    C++ class static member initialization

  15. 15

    Forcing initialization of static data member of template class

  16. 16

    Static final member initialization on private class

  17. 17

    List initialization of static member of template class

  18. 18

    Forcing initialization of static data member of template class

  19. 19

    Whether redeclare a const static variable outside a class or not

  20. 20

    Difference between a const inside a proc vs outside

  21. 21

    Class scope constants: const vs static const

  22. 22

    Why we declare static variable in a class & the definition in outside of the class?

  23. 23

    Initialize static const member of a class, where the member is of a private type?

  24. 24

    std::bind a static member function inside the class

  25. 25

    static const variable inside a class used as template

  26. 26

    Initialization of static data member

  27. 27

    Definition of the static data member

  28. 28

    Initialization of a static const variable

  29. 29

    Builder Pattern inside vs outside class?

HotTag

Archive