简体   繁体   English

如何阅读以下主要代码? 我不知道这个

[英]How to read the following code for main? I do not know this

How to read the following code for main?如何阅读以下主要代码? I do not know this我不知道这个

Code:代码:

class one
{
public:
    void operator()() const
    {
        f();
        f1();
    }
};

I want to call the operator To main?我想打电话给接线员 To main?

void operator()() const defines a function call operator , which can be used as: void operator()() const定义了一个function 调用 operator ,可以用作:

    one ob;

    ob(); // calls ob.operator()()

For another, more complete, example.另一个更完整的例子。

#include <iostream>

class Two
{
public:
    int operator()(const char *str) const
    {
        std::cout << "operator() called with " << str << std::endl;
        return 101;
    }
};

int main()
{
    Two two;
    int n = two("'test'");
    std::cout << "operator() returned " << n << std::endl;
}

Output: Output:

operator() called with 'test'
operator() returned 101

You can create an instance of the class in the main function and call the function using that instance.您可以在主 function 中创建 class 的实例,并使用该实例调用 function。

class one
{
public:
    void operator()() const
    {
        f();
        f1();
    }
};

int main() {
one obj_one;

// calling the member function -> method
obj_one.operator()();

return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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