C++ 如何将cpp文件中的静态函数公开给其他文件

C++ 如何将cpp文件中的静态函数公开给其他文件,c++,function,static,C++,Function,Static,在hello.cpp文件中,我有一个静态函数 static void hello() { std::cout << "hello" << std::endl; } 在这种情况下,向其他文件公开hello()的最推荐方法是什么?如果它在类的公共范围内,我们可以使用范围解析操作符(::)访问静态函数,而不必初始化对象 class Hello { public: static void Hello1() { printf("Hello\

hello.cpp
文件中,我有一个静态函数

static void hello()
{
    std::cout << "hello" << std::endl;
}

在这种情况下,向其他文件公开
hello()
的最推荐方法是什么?

如果它在类的公共范围内,我们可以使用范围解析操作符(::)访问静态函数,而不必初始化对象

class Hello
{
public:
    static void Hello1()
    {
        printf("Hello\n");
    }

};
然后从另一个类ie World.cpp(记住包含hello.h文件)

阶级世界
{
公众:
世界(){
Hello::Hello1();

如果它在类的公共范围内,我们可以使用范围解析操作符(::)来访问静态函数,而不必初始化对象

class Hello
{
public:
    static void Hello1()
    {
        printf("Hello\n");
    }

};
然后从另一个类ie World.cpp(记住包含hello.h文件)

阶级世界
{
公众:
世界(){
Hello::Hello1();

std::cout这样使用,关键字
static
使函数的链接“内部”。 这意味着
hello()
仅在
hello.cpp
中可见,即使您向其他编译单元声明了它

例如,下面的代码生成链接错误(未解析的外部引用):

hello.cpp:

#include <iostream>

static void hello()
{
    std::cout << "hello" << std::endl;
}
main.cpp:

#include "hello.h"

void main()
{
    hello(); // But ouch, it's not resolved! The linker can't access to the code you wrote in hello.cpp due to the fact hello() is static!
}
#include <iostream>
#include "otherFile.h"

void main()
{
    void * pf = hello; // Gives 0x01181810 during the test I'm currently doing while writing

    tryWithAnotherCppFile();
}
因此,根据定义,您不能以这种方式公开您的函数

现在,如果在从
hello.cpp
中删除
hello()
的代码后,声明函数
static
,并直接在其头文件中实现它:

你好,h:

#pragma once

void hello(); // OK, it's declared
#pragma once

static void hello() 
{
    std::cout << "hello" << std::endl;
}
#pragma once

inline void hello() 
{
    std::cout << "hello" << std::endl;
}
otherFile.h:

#pragma once

void tryWithAnotherCppFile();
otherFile.cpp:

#include "otherFile.h"
#include "hello.h"

void tryWithAnotherCppFile()
{
    void * pf = hello; // Here it gives 0x01181d40, which is different!
}
现在,用以下方法更改
hello.h
,将
hello()
声明为
inline
,而不是
static

你好,h:

#pragma once

void hello(); // OK, it's declared
#pragma once

static void hello() 
{
    std::cout << "hello" << std::endl;
}
#pragma once

inline void hello() 
{
    std::cout << "hello" << std::endl;
}
#pragma一次
内联void hello()
{

std::cout这样使用,关键字
static
使函数的链接“内部”。 这意味着
hello()
仅在
hello.cpp
中可见,即使您向其他编译单元声明了它

例如,下面的代码生成链接错误(未解析的外部引用):

hello.cpp:

#include <iostream>

static void hello()
{
    std::cout << "hello" << std::endl;
}
main.cpp:

#include "hello.h"

void main()
{
    hello(); // But ouch, it's not resolved! The linker can't access to the code you wrote in hello.cpp due to the fact hello() is static!
}
#include <iostream>
#include "otherFile.h"

void main()
{
    void * pf = hello; // Gives 0x01181810 during the test I'm currently doing while writing

    tryWithAnotherCppFile();
}
因此,根据定义,您不能以这种方式公开您的函数

现在,如果在从
hello.cpp
中删除
hello()
的代码后,声明函数
static
,并直接在其头文件中实现它:

你好,h:

#pragma once

void hello(); // OK, it's declared
#pragma once

static void hello() 
{
    std::cout << "hello" << std::endl;
}
#pragma once

inline void hello() 
{
    std::cout << "hello" << std::endl;
}
otherFile.h:

#pragma once

void tryWithAnotherCppFile();
otherFile.cpp:

#include "otherFile.h"
#include "hello.h"

void tryWithAnotherCppFile()
{
    void * pf = hello; // Here it gives 0x01181d40, which is different!
}
现在,用以下方法更改
hello.h
,将
hello()
声明为
inline
,而不是
static

你好,h:

#pragma once

void hello(); // OK, it's declared
#pragma once

static void hello() 
{
    std::cout << "hello" << std::endl;
}
#pragma once

inline void hello() 
{
    std::cout << "hello" << std::endl;
}
#pragma一次
内联void hello()
{

std::cout静态函数的全部要点是静态链接。您需要删除
static
,使其在linking@Tyker你什么意思?我不能打电话给你好
来自其他文件,如果它有一个
静态
关键字?您不需要在函数前面应用
静态
。您试图解决的问题是什么?@ZackLee
静态
正是为了防止从其他文件调用函数。静态函数的整个要点是静态链接。您需要要删除
静态
,使其在linking@Tyker你什么意思?我不能打电话给你好
来自其他文件,如果它有一个
static
关键字?您不需要在函数前面应用
static
。您试图解决的问题是什么?@ZackLee
static
正是为了防止从其他文件调用函数。您认为哪一个是更好的解决方案1)将函数仅在标题中使用ion作为
inline
.2)创建类并在其中使用静态函数,然后使用
classname::hello()调用它
来自其他文件。@ZackLee这实际上取决于您需要做什么。为什么您的函数需要是静态的?为什么它需要是内联的?它不能只在its.h中声明并在its.cpp中实现,以便像往常一样与所有包含hello.h的人共享吗?在您最初的问题中,与面向对象的pro没有任何关系编程,但取决于您实际开发的内容,将此函数定义为静态类成员可能有一定的意义(因此应用于类而不是其实例的函数),但我们这里没有足够的元素来确定这一点。如果我没有在标头中将函数定义为静态函数,它会显示函数的冲突类型。我想这是因为我有使用相同名称但不同参数的函数。@ZackLee重载可能。如果它们只因返回类型不同而不同,则重载函数会被视为冲突函数,因为编译器无法决定采用哪一种。我认为你应该重新审视你的应用程序的设计。如果在实践中遇到问题后从头开始重新设计你的软件,这并不丢脸。我也建议阅读:你认为哪一种解决方案更好1)将函数作为
inline
.2)创建类并在其中使用静态函数,然后使用
classname::hello()调用它
来自其他文件。@ZackLee这实际上取决于您需要做什么。为什么您的函数需要是静态的?为什么它需要是内联的?它不能只在its.h中声明并在its.cpp中实现,以便像往常一样与所有包含hello.h的人共享吗?在您最初的问题中,与面向对象的pro没有任何关系编程,但取决于您实际开发的内容,将此函数定义为静态类成员可能有一定的意义(因此应用于类而不是其实例的函数),但我们这里没有足够的元素来确定这一点。如果我在头中没有将函数定义为静态函数,则表示函数的类型冲突。我认为这是因为我有使用相同名称但参数不同的函数。@ZackLee可能重载。如果它们只因返回类型重载不同而不同,则重载