In C ++ (class), do I always need to declare a function in the header file?

For example, I have a markerdisplay.cpp file. The marker member function will look like this.

void MarkerDisplay::setMarkerStatus(MarkerID id, StatusLevel level, const std::string& text)
        {
               .....
        }

Can I use the non-member function in markerdisplay.cpp?

For instance,

bool validateFloats(const visualization_msgs::Marker& msg)
        {
              ...
        }

The validateFloats function is not a member function, and I also do not declare it in the header file. I use this function only inside the validateFloats.cpp file.

Someone told me that this may cause some problems. It's true?

+4
source share
2 answers

.cpp, . , , "", .

, , "project-public" , .

, static:

static bool validateFloats(const visualization_msgs::Marker& msg);

:

namespace {
    bool validateFloats(const visualization_msgs::Marker& msg);
}

(), .

+10

: non member markerdisplay.cpp, ...

, .

, API.

( )

namespace {
    bool validateFloats(const visualization_msgs::Marker& msg) {
       // ...
    }
}

static :

static bool validateFloats(const visualization_msgs::Marker& msg) {
   // ...
}
+6

Source: https://habr.com/ru/post/1672207/


All Articles