Hi
I have a question. The aswer is very short I think. I got many different useful helps in different forums but still my main question is not answered clearly!

MY QUESTION:
Can a not inline method have internal linkage?

This question is simple & clear (if you don't think that I may mean something else).

But let me give you an example in which I need the answer of this question. This is just an example of where I need the answer. It is not my main issue. And I have other examples where the aswer is needed.

by this example I want only to show how important my question is:

Example:
Suppose I have a project with only two .cpp files:

console.cpp:

#include <iostream>
using namespace std;
class A
{
     void f();
};
void A::f()
{
     cout<< "hello";
}
 
int main()
{ 
}

unit1.cpp:

#include <iostream>
using namespace std;
class A
{
     void f();
};
void A::f()
{
     cout<< "bye";
}

In this project we have two .cpp files. Each .cpp file has a method with name A::f(). The method in Each file is independent, that is, each translation unit has its own definition for A::f().

This project can be compiled without errors. But it can't be linked. It has this linker error(s):

Error	1	error LNK2005: "private: void __thiscall A::f(void)" (?f@A@@AAEXXZ) already defined in console.obj	unit1.obj
Error	2	fatal error LNK1169: one or more multiply defined symbols found	G:\Important Files\My Documents\Visual Studio 2008\Projects\console app\Release\console app.exe	1

This error can be solved if A::f() has internal linkage.
If A::f() was not a method and was a global function then I could use the keyword static to let it have internal linkage & then there was no linker errors. But it is not a global function. It is a method.
How can I give internal linkage to a method?

Note: There are many different ways to avoid this linker error. For example I can rename one A::f(). Or I can inline A::f(). Or I can use namespaces. Or I can use a Borland linker. But my question is not how solve this error.

My question is: How can I make the likage internal?

Can a method have have internal linkage?

I hope someone can answer my question a clear as I said!

(Why the daniweb editor is not rich text?! I remember it was before)

You are defining the class twice. Surely it cannot link.


>>(Why the daniweb editor is not rich text?! I remember it was before)
So as to make it tough for you to use color on every alternate word. Phew man! reading you post needed courage.

So as to make it tough for you to use color on every alternate word.

you see that this does not!

reading you post needed courage.

And answering it needs more courage!


You are defining the class twice. Surely it cannot link.

All those colors were to convice you not to post such a naive answer! Have courage & read carefully before answering if you really think you can!
I wish you better posts!

commented: If there was just enough room for an ASCII middle finger..... -1
commented: fail -2

All those colors were to convice you not to post such a naive answer!

All these colors make me not want to help you.
You're lucky I'm so bored that I'll actually give it a shot.
Here you go:

Use namespaces.

as ~s.o.s.~ would put it: kthxbye!

The answer: use anonymous namespace...

using std::cout;

class A // External linkage name
{
public:
    void f() { cout << "::A:f()\n"; }
};
namespace { // Internal linkage area:
    class A // Internal linkage name
    {
    public:
        void f() { cout << "LocalA::f()\n"; }
    };
    typedef A LocalA; // for local references
    void F() 
    {
        cout << "F()...\n"; 
        A a; a.f(); ::A aa; aa.f(); 
    }
}
// need type alias if ::A is visible here.
void FF() { cout << "FF()...\n"; LocalA a; a.f(); }

int main()
{
    F();
    FF();
	return 0;
}

Try this method for separate modules.
Moreover: anonymous namespace is a preferred way to force internal linkage in C++ (instead of C-style static keyword).

commented: Finally someone which just answers his question :) +4

@ArkM
Thank you very much.
I didn't know anything about anonymous namespaces.
& I didn't know that they make the class (or the methods in it) have internal linkage.
You really helped me.
rep++

All these colors make me not want to help you.
You're lucky

And you're sooo lucky too
cos I wanted to add sprinckles of blinking colored asterisks all over my first post & I did not (lol :lol:).
thank you! although I needed some more info!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.