简体   繁体   中英

How functions executed declared inside class declaration in c++?

Some code contains function invoked inside class declaration.

class Example
{
   public:
    bool keyBlobClosed = deviceClosed()?true:false;
};

In this case, deviceClosed() function gets executed when object created or when class declaration created in memory?

This is called a default member initializer . It is allowed on non-static members (as you have used here) since C++11.

It is equivalent to providing a member initializer list that performs the same initialization:

class Example {
public:
    bool keyBlobClosed;
    Example() 
        : keyBlobClosed(deviceClosed())
    {
    }
};

Obviously, this will cause deviceClosed() to be called whenever Example is instantiated. The return value of deviceClosed() will be used to initialize keyBlobClosed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM