Multiple class definition

I am writing utility functions for my current project.

Right now I have a .h utility header that defines a class with static methods:

#pragma once

class Utils
{
public:
    static int firstFunc()
    {
       return 0;
    }

    static bool secondFunc()
    {
      return false;
    }
};

This header is included every time I need to use such functions (currently in two translation units), and it works fine.

Now, after reviewing the code, it has been proposed to replace this class with C-style functions. My naive first attempt:

#pragma once

int firstFunc()
{
    return 0;
}

bool secondFunc()
{
    return false;
}

failed to bind, returning a multiple definition of a function error. I understand why this happens: the utils.h header, which contains function definitions, is present in two different compilation units: the linker does not know which definition to use.

, , utils.cpp .

: , , ?

+4
1

( ), , inline. :

#pragma once

class Utils
{
public:
    static int firstFunc();

    static bool secondFunc();
};

static int Utils::firstFunc()
{
   return 0;
}

static bool Utils::secondFunc()
{
  return false;
}

... .

, " C", ( ) . ++ ;)

+2

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


All Articles