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 Dev

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

From Dev

Scope of an object created in an if-condition 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

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

From Dev

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

From Dev

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

From Dev

Definition of class in c++

From Dev

Definition of object in C++

From Dev

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

From Dev

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

From Java

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

From Dev

Access to class object during class definition

From Dev

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

From Java

Local variables definition in relation to scope, C++

From Dev

Instantiation an Object Scope in a Class in Java

From Dev

'Class Instance Object' was not declared in this scope

From Dev

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

From Dev

Access an object created in another class

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

Class definition conventions in C#

From Dev

JavaScript class definition local scope enable global code completion with tern

From Dev

JavaScript class definition local scope enable global code completion with tern

From Dev

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

From Dev

Python How to Initialize Reader Object in Class Definition

From Dev

Why Object Class definition makes difference

From Dev

Why Object Class definition makes difference

From Dev

Python How to Initialize Reader Object in Class Definition

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

    Definition of class in c++

  9. 9

    Definition of object in C++

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

    Access to class object during class definition

  14. 14

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

  15. 15

    Local variables definition in relation to scope, C++

  16. 16

    Instantiation an Object Scope in a Class in Java

  17. 17

    'Class Instance Object' was not declared in this scope

  18. 18

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

  19. 19

    Access an object created in another class

  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

    Class definition conventions in C#

  23. 23

    JavaScript class definition local scope enable global code completion with tern

  24. 24

    JavaScript class definition local scope enable global code completion with tern

  25. 25

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

  26. 26

    Python How to Initialize Reader Object in Class Definition

  27. 27

    Why Object Class definition makes difference

  28. 28

    Why Object Class definition makes difference

  29. 29

    Python How to Initialize Reader Object in Class Definition

HotTag

Archive