C++ - Scope of the object created at the end of class definition

Deepika

If I have a class like

class sample{
  // ...
} obj;

What is the scope of object obj created above? When will the object be destroyed?

R Sahu

The scope of obj is the same as the scope of the class definition. If you define it outside of all functions, it's scope will be global and it will be created and initialized at static initialization time and destroyed as part of program termination.

However, it is also possible to define such an object in a function. In that case, it will be created when the function is entered and destroyed when the function returns.

void test()
{
   class sample{ }obj;
}

is perfectly valid code.

You can also define it as a nested type in a class. In that case, obj will be a member variable of the class. It will be created and destroyed with the construction and destruction of objects of the containing class.

struct Foo
{
   class sample{ }obj;
};

Also (thanks are due to @sjdalessandro for pointing it out), if the object is defined with global scope in a library and this library is loaded dynamically, then the object is created when the library is loaded (which is not necessarily when the program starts) and is destroyed when the library is unloaded (which is not necessarily when the program exits).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Local variables definition in relation to scope, C++

From Java

Hilt: Why is a class in ApplicationComponent scope created twice?

From Dev

Instantiation an Object Scope in a Class in Java

From Dev

Scope of an object created in an if-condition in C++

From Dev

Definition of object in C++

From Dev

In Objective-C, when are metaclass object and class object created?

From Dev

Class definition conventions in C#

From Dev

Why is the semicolon not required but allowed at the end of a class definition?

From Dev

Why Object Class definition makes difference

From Dev

JavaScript class definition local scope enable global code completion with tern

From Dev

Scope of struct object in c++ created without new keyword

From Dev

Python How to Initialize Reader Object in Class Definition

From Dev

Definition of member functions in the scope of the enclosing class for a nested class

From Dev

C++ - Scope of the object created at the end of class definition

From Dev

How many methods in a class will be created in memory when an object is created in C#?

From Dev

Scope of an object created in an if-condition in C++

From Dev

C++ Array Class Definition and Implementation, Not Declared in Scope?

From Dev

Error when compiling shared object: C++ class definition not being detected

From Dev

Definition of class in c++

From Dev

AngularJS: can't push $scope object onto end b/c data keeps updating

From Dev

Why does c++ program crash after temporary object is destroyed at end of scope

From Dev

Why Object Class definition makes difference

From Dev

JavaScript class definition local scope enable global code completion with tern

From Dev

Python How to Initialize Reader Object in Class Definition

From Dev

Access an object created in another class

From Dev

'Class Instance Object' was not declared in this scope

From Dev

C++ Array of an object (created) inside another Class

From Dev

Access to class object during class definition

From Dev

Should a class definition be put in the global or local scope?

Related Related

  1. 1

    Local variables definition in relation to scope, C++

  2. 2

    Hilt: Why is a class in ApplicationComponent scope created twice?

  3. 3

    Instantiation an Object Scope in a Class in Java

  4. 4

    Scope of an object created in an if-condition in C++

  5. 5

    Definition of object in C++

  6. 6

    In Objective-C, when are metaclass object and class object created?

  7. 7

    Class definition conventions in C#

  8. 8

    Why is the semicolon not required but allowed at the end of a class definition?

  9. 9

    Why Object Class definition makes difference

  10. 10

    JavaScript class definition local scope enable global code completion with tern

  11. 11

    Scope of struct object in c++ created without new keyword

  12. 12

    Python How to Initialize Reader Object in Class Definition

  13. 13

    Definition of member functions in the scope of the enclosing class for a nested class

  14. 14

    C++ - Scope of the object created at the end of class definition

  15. 15

    How many methods in a class will be created in memory when an object is created in C#?

  16. 16

    Scope of an object created in an if-condition in C++

  17. 17

    C++ Array Class Definition and Implementation, Not Declared in Scope?

  18. 18

    Error when compiling shared object: C++ class definition not being detected

  19. 19

    Definition of class in c++

  20. 20

    AngularJS: can't push $scope object onto end b/c data keeps updating

  21. 21

    Why does c++ program crash after temporary object is destroyed at end of scope

  22. 22

    Why Object Class definition makes difference

  23. 23

    JavaScript class definition local scope enable global code completion with tern

  24. 24

    Python How to Initialize Reader Object in Class Definition

  25. 25

    Access an object created in another class

  26. 26

    'Class Instance Object' was not declared in this scope

  27. 27

    C++ Array of an object (created) inside another Class

  28. 28

    Access to class object during class definition

  29. 29

    Should a class definition be put in the global or local scope?

HotTag

Archive