如何“全球化”结构体方法?

问题描述 投票:0回答:1

我有一个结构/类,我想全局调用它的一些(或全部)方法(无需每次都编写 class_name::func_name() 。我不知道在

using namespace foo;
行上替换什么。我想要foo 保留为结构/类,我不想更改结构本身。

#include <stdio.h>

struct foo
{
    struct point
    {
        ll x, y;
    };

    inline void say_hi()
    {
        printf("hi\n");
    }
};

// using namespace foo; // this obviously doesn't work, foo is not a namespace
// using foo:foo;                           // none of them work
// using foo::point(); using foo::say_hi(); //

int main ()
{
    point p;
    say_hi();

    return 0;
}

注释中的 3 行无法编译。有什么办法可以滥用

using
,或者有其他关键字吗?

c++ class global using
1个回答
0
投票

您的结构方法是非静态,因此需要在结构的实例上调用,即:

p.say_hi();


你不能按照你想要的方式全球化它(即使有这样的语法。但实际上没有)。

© www.soinside.com 2019 - 2024. All rights reserved.