Static function returning a static instance of the class - shouldn't the instance be the same?

user2018675

I have the following code:

#include <iostream>
using namespace std;

class Test {
public:
  static Test& get() {
    static Test testSing;
    return testSing;
  }
};

int main() {
  Test a = Test::get();
  Test b = Test::get();

  cout << &a << endl;
  cout << &b << endl;
}

I thought that a and b should have the same address, since I thought they should be constructed only once. However, I get different memmory addresses in this test.

Is there something trivial that I am missing? Shouldn't they have the same address?

Jarod42

You use the copy constructor, so you have 2 different objects, references are missing.

You probably want

Test& a = Test::get();
Test& b = Test::get();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Static Instance Base/Derived class

来自分类Dev

Accessing static methods from instance in Typescript

来自分类Dev

为什么Enums中的static和instance init块的行为与Class中的不同

来自分类Dev

How to access static members from instance methods in typescript?

来自分类Dev

python中的@instance和@static方法有什么区别?

来自分类Dev

C++ type of enclosing class in static member function

来自分类Dev

Java: [Global method access] enum with a single instance vs on-demand object construction vs static

来自分类Dev

create an instance of class in python 2.7

来自分类Dev

Access class variable from instance

来自分类Dev

May static and non static variables have the same name

来自分类Dev

Why an instance of inherited class can't access to protected member of base class in different package

来自分类Dev

Does this point to the class or a class instance? (6)

来自分类Dev

forcing clang to emit code for never-referenced static member function of class-template instantiation

来自分类Dev

Comparing two delegate objects are same instance

来自分类Dev

static variables can not be accessed by other class

来自分类Dev

Why I can instantiate a static inner class?

来自分类Dev

Is it true that every inner class requires an enclosing instance?

来自分类Dev

WM_CLASS与WM_INSTANCE?

来自分类Dev

Does function parameter allow static specifier?

来自分类Dev

Invalid use of this in a non static member function

来自分类Dev

C++ static variable in member function

来自分类Dev

Laravel Twitter Library - Where is the static function getUserTimeline()

来自分类Dev

what is by mean an instance? or instance of?

来自分类Dev

为什么是typeof(T)!= instance.getType()?

来自分类Dev

Call derived class method from base class instance

来自分类Dev

为什么std :: forward返回static_cast <T &&>而不返回static_cast <T>?

来自分类Dev

Serving Static Content from same directory as service - Dropwizard

来自分类Dev

static_cast <T>对T&有何作用?

来自分类Dev

Invalid XPath: string-join(.//div[@class="static"]/text(), "")?

Related 相关文章

  1. 1

    Static Instance Base/Derived class

  2. 2

    Accessing static methods from instance in Typescript

  3. 3

    为什么Enums中的static和instance init块的行为与Class中的不同

  4. 4

    How to access static members from instance methods in typescript?

  5. 5

    python中的@instance和@static方法有什么区别?

  6. 6

    C++ type of enclosing class in static member function

  7. 7

    Java: [Global method access] enum with a single instance vs on-demand object construction vs static

  8. 8

    create an instance of class in python 2.7

  9. 9

    Access class variable from instance

  10. 10

    May static and non static variables have the same name

  11. 11

    Why an instance of inherited class can't access to protected member of base class in different package

  12. 12

    Does this point to the class or a class instance? (6)

  13. 13

    forcing clang to emit code for never-referenced static member function of class-template instantiation

  14. 14

    Comparing two delegate objects are same instance

  15. 15

    static variables can not be accessed by other class

  16. 16

    Why I can instantiate a static inner class?

  17. 17

    Is it true that every inner class requires an enclosing instance?

  18. 18

    WM_CLASS与WM_INSTANCE?

  19. 19

    Does function parameter allow static specifier?

  20. 20

    Invalid use of this in a non static member function

  21. 21

    C++ static variable in member function

  22. 22

    Laravel Twitter Library - Where is the static function getUserTimeline()

  23. 23

    what is by mean an instance? or instance of?

  24. 24

    为什么是typeof(T)!= instance.getType()?

  25. 25

    Call derived class method from base class instance

  26. 26

    为什么std :: forward返回static_cast <T &&>而不返回static_cast <T>?

  27. 27

    Serving Static Content from same directory as service - Dropwizard

  28. 28

    static_cast <T>对T&有何作用?

  29. 29

    Invalid XPath: string-join(.//div[@class="static"]/text(), "")?

热门标签

归档