Do classes have external linkage?

后端 未结 5 2088
梦谈多话
梦谈多话 2020-12-16 01:00

I have 2 files A.cpp and B.cpp which look something like

A.cpp
----------
class w
{
public:
    w();
};


B.cpp
-----------
class w
{
public:
    w();
};
         


        
5条回答
  •  太阳男子
    2020-12-16 01:45

    Technically, as Maxim points out, linkage applies to symbols, not to the entities they denote. But the linkage of a symbol is partially determined by what it denotes: symbols which name classes defined at namespace scope have external linkage, and w denotes the same entity in both A.cpp and B.cpp.

    C++ has two different sets of rules concerning the definition of entities: some entities, like functions or variables, may only be defined once in the entire program. Defining them more than once will result in undefined behavior; most implementations will (most of the time, anyway) give a multiple definition error, but this is not required or guaranteed. Other entities, such as classes or templates, are required to be defined in each translation unit which uses them, with the further requirement that every definition be identical: same sequence of tokens, and all symbols binding to the same entity, with a very limited exception for symbols in constant expressions, provided the address is never taken. Violating these requirements is also undefined behavior, but in this case, most systems will not even warn.

提交回复
热议问题