Declaring static class variables

vico

I have class with static string variable m.

class turbo
 {
 public:
 static string m;
 }

if I not declare like defined below I will get error undefined reference turbo::m.

string turbo::m;

Why I should declare it twice? I don't need to declare non static variables second time. Where is logic?

Tomer W

To be honest, I was surprised by this too the first time,
but found the very reasonable reason for it.

while member variables are stored within the object allocated space, static members are not placed within the object memory space (either heap or stack)
but have a private shared space. just like functions do.

Therefore, declaring the static string m within the class. is just like saying: there is a shared definition of this somewhere and the name is turbo::m.

Hope this clears things up.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Java static class import in the declaring class

分類Dev

C++ declaring a static object in a class

分類Dev

C++ declaring static enum vs enum in a class

分類Dev

Singleton VS static(class) variables

分類Dev

Are Ruby class variables similar to the Java static variables?

分類Dev

declaring static dictionary in Java

分類Dev

Static vs class functions/variables in Swift classes?

分類Dev

C++ static variables in static class methods defined in headers

分類Dev

Recursively declaring variables - python

分類Dev

Error with declaring variables in for loop

分類Dev

C - Declaring variables and calling malloc

分類Dev

(Why) can we assign non-static class members to static variables in initialization?

分類Dev

Declaring variables inside or outside in a for-in loop

分類Dev

Netsuite : Advanced PDF / HTML Template - Declaring Variables

分類Dev

Declaring Variables and Using Them Elsewhere with Jquery

分類Dev

Dynamically declaring Variable inside a class in php

分類Dev

Parse JSON without declaring model class

分類Dev

Declaring variable inside functions of a class in C++

分類Dev

Edit CSS class when declaring the div

分類Dev

Are static variables inherited

分類Dev

Static variables initialization process

分類Dev

static class initialization with static references

分類Dev

Declaring properties in parent class that are defined in child class in TypeScript

分類Dev

speed of declaring element with class name vs. class name

分類Dev

ES6/JavaScript - declaring variables without let/const

分類Dev

Declaring variables on Google sheet script editor using let

分類Dev

program returning garbage values even after declaring variables in c

分類Dev

Why use underscore kotlin convention when declaring variables in view model

分類Dev

Static class stored as a property

Related 関連記事

  1. 1

    Java static class import in the declaring class

  2. 2

    C++ declaring a static object in a class

  3. 3

    C++ declaring static enum vs enum in a class

  4. 4

    Singleton VS static(class) variables

  5. 5

    Are Ruby class variables similar to the Java static variables?

  6. 6

    declaring static dictionary in Java

  7. 7

    Static vs class functions/variables in Swift classes?

  8. 8

    C++ static variables in static class methods defined in headers

  9. 9

    Recursively declaring variables - python

  10. 10

    Error with declaring variables in for loop

  11. 11

    C - Declaring variables and calling malloc

  12. 12

    (Why) can we assign non-static class members to static variables in initialization?

  13. 13

    Declaring variables inside or outside in a for-in loop

  14. 14

    Netsuite : Advanced PDF / HTML Template - Declaring Variables

  15. 15

    Declaring Variables and Using Them Elsewhere with Jquery

  16. 16

    Dynamically declaring Variable inside a class in php

  17. 17

    Parse JSON without declaring model class

  18. 18

    Declaring variable inside functions of a class in C++

  19. 19

    Edit CSS class when declaring the div

  20. 20

    Are static variables inherited

  21. 21

    Static variables initialization process

  22. 22

    static class initialization with static references

  23. 23

    Declaring properties in parent class that are defined in child class in TypeScript

  24. 24

    speed of declaring element with class name vs. class name

  25. 25

    ES6/JavaScript - declaring variables without let/const

  26. 26

    Declaring variables on Google sheet script editor using let

  27. 27

    program returning garbage values even after declaring variables in c

  28. 28

    Why use underscore kotlin convention when declaring variables in view model

  29. 29

    Static class stored as a property

ホットタグ

アーカイブ