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

Benno

In the following example

void fun() {
    if(int i=SOME_VALUE) {
        // ...
    } else {
        // ...
    }
}

What is the scope of i? Can we use it inside the if-block? Can we use it inside the else-block?

Edit:

As a follow-up, in this modified example

void fun() {
    if(int i=SOME_VALUE) {
        // ...
    } else if(int j=SOME_OTHER_VALUE){
        // ...
    } else {
        // ...
    }
}

Can we access both i and j in the else clause?

Kerrek SB

Yes, and yes.

A typical use for this is dynamic casting:

if (auto p = dynamic_cast<Derived*>(base_pointer))
{
    // p is a Derived*
}
else
{
    // not the right dynamic type
}

Another construction I've been finding useful:

if (auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(std::fopen("file.txt", "rb"), std::fclose))
{
    // file exists, use fp.get()
}
else
{
    // file does not exist
}

And one more:

if (pid_t pid = fork())
{
    waitpid(pid, nullptr, 0);
}
else
{
    execl("/bin/rm", "/bin/rm", "-rf", "/", static_cast<char*>(nullptr));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

C++ Object out of scope

From Dev

C++ Object Pointers and Scope

From Dev

C# Object scope (easymodbus)

From Dev

Object lifetime when created and used only in the scope of a using statement

From Dev

How will ReentrantLock object created inside a method's local scope work?

From Dev

Slim Framework - PDO object created but seems not inside the scope

From Dev

django - make sure one related object is created, race condition

From Dev

Error: 'object' was not declared in this scope (C++)

From Dev

Does this object exist? c++ scope/inheritance

From Dev

What will a pointer to a object point to outside the object scope in C++

From Dev

COM object created by CComObject::CreateInstance not destroyed when pointer goes out of scope

From Dev

Add condition to join linq to object c#

From Dev

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

From Dev

How to push object pointer to vector when object is created in C++?

From Dev

Pass created object as argument, from inside that object, C++

From Dev

OR condition under scope in Yii

From Dev

Scope of variable declared in condition

From Dev

Is it good practice to declare object inside block scope(Do and Loop,For [Each] and Next,If and End If etc) Condition

From Dev

Rails scope created within month

From Dev

C++: Destructor being called outside object scope?

From Dev

Create Python object in local scope from within C++

From Dev

C++ conditional storage allocation for object caused by scope

From Dev

C# CA2000 Dispose Object Before Losing Scope

From Dev

C++ conditional storage allocation for object caused by scope

From Dev

Is it safe to use C++'s std::move from object in inner scope to outer scope?

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

    C++ Object out of scope

  6. 6

    C++ Object Pointers and Scope

  7. 7

    C# Object scope (easymodbus)

  8. 8

    Object lifetime when created and used only in the scope of a using statement

  9. 9

    How will ReentrantLock object created inside a method's local scope work?

  10. 10

    Slim Framework - PDO object created but seems not inside the scope

  11. 11

    django - make sure one related object is created, race condition

  12. 12

    Error: 'object' was not declared in this scope (C++)

  13. 13

    Does this object exist? c++ scope/inheritance

  14. 14

    What will a pointer to a object point to outside the object scope in C++

  15. 15

    COM object created by CComObject::CreateInstance not destroyed when pointer goes out of scope

  16. 16

    Add condition to join linq to object c#

  17. 17

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

  18. 18

    How to push object pointer to vector when object is created in C++?

  19. 19

    Pass created object as argument, from inside that object, C++

  20. 20

    OR condition under scope in Yii

  21. 21

    Scope of variable declared in condition

  22. 22

    Is it good practice to declare object inside block scope(Do and Loop,For [Each] and Next,If and End If etc) Condition

  23. 23

    Rails scope created within month

  24. 24

    C++: Destructor being called outside object scope?

  25. 25

    Create Python object in local scope from within C++

  26. 26

    C++ conditional storage allocation for object caused by scope

  27. 27

    C# CA2000 Dispose Object Before Losing Scope

  28. 28

    C++ conditional storage allocation for object caused by scope

  29. 29

    Is it safe to use C++'s std::move from object in inner scope to outer scope?

HotTag

Archive