小编Pou*_*dXI的帖子

调用同名函数

是否可以从Foo.cpp调用foo()函数而不将函数名Foo :: foo()更改为Foo :: newfoo().

main.cpp中

#include <iostream>
#include "Foo.hpp"

class Foo {
public:
    void foo() {
        std::cout << "Foo::foo\n";
    }
    void bar() {
        std::cout << "Foo::bar\n";
        foo();// need to call foo() function from foo.cpp not Foo::foo()
    }
};

int main () {
    Foo f;
    f.bar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Foo.hpp

#ifndef FOO_HPP_INCLUDED
#define FOO_HPP_INCLUDED

void foo();

#endif // FOO_HPP_INCLUDED
Run Code Online (Sandbox Code Playgroud)

Foo.cpp中

#include "Foo.hpp"
#include <iostream>
void foo(){
    std::cout << "foo\n";
}
Run Code Online (Sandbox Code Playgroud)

ps.sorry为我可怜的英语.

c++ class function include duplicates

7
推荐指数
1
解决办法
3406
查看次数

标签 统计

c++ ×1

class ×1

duplicates ×1

function ×1

include ×1