Depending on the order of creating the objects, the arguments of the overridden operator, what will be the output of the following code?
#include <memory>
#include <iostream>

class SomeObj
{
public:
    SomeObj() {}
    SomeObj(const int& val)  : val_(std::make_shared<int>(val))
    {
        std::cout << *val_;
    }
    void set(const int& val)
    {
        val_ = std::make_shared<int>(val);
    }
    friend bool operator&&(const SomeObj&, const SomeObj&);
private:
    std::shared_ptr<int> val_;
};

bool operator&&(const SomeObj& lhs, const SomeObj& rhs)
 {
     return lhs.val_ && rhs.val_;
 }

int main() {
    if (SomeObj(1) && SomeObj(2)) {
        std::cout << 3;
    }
    return 0;
}
Explanation
The order of construction of SomeObj(1) and SomeObj(2) for user defined functions (including the overloaded operators) is not defined.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes