C ++高级拼图[关闭]

问题描述 投票:-4回答:2

我遇到了以下问题,我没有一个好主意。也许有人可以帮助我?

class Foo 
{
    // [Your code goes here] 
    // Rec1: Don’t write function call operator()
    // Rec2: Don’t use macros.
}; 
int main()
{
    Foo foo;
    foo();          // This line prints “Hello World!” 
}
c++ puzzle
2个回答
2
投票
#include <iostream>
class Foo
{
public:
    using fncPntr = void(*)(void);
    //Implicit conversion to a function pointer
    operator fncPntr()
    {
        return &Print;
    }
    void static Print()
    {
        std::cout << "Hello World!";
    }
};
int main()
{
    Foo foo;
    foo();
}

-3
投票
class Foo{
// [Your code goes here] 
};
void foo()
{
    std::cout << "hello";
}
class Bar{
// Rec1: Don’t write function call operator()
// Rec2: Don’t use macros.
};
© www.soinside.com 2019 - 2024. All rights reserved.