Two Questions!

Pages: 12
External Linkage means it can be accessed from other translation units and internal means it can only be accessed in one?

Is that correct?
Are you sure that its only for one translation unit? Cause wouldnt you get linker errors if it is also included in another file cause it would violate ODR?


Header.h:
1
2
3
4
5
6
7
8
9
#pragma once

unsigned getFunctionCount() ;

unsigned function()
{
    static unsigned count = 0 ;
    return count++ ;
}


Source.cpp:
1
2
3
4
5
6
#include "Header.h"

unsigned getFunctionCount()
{
    return function() ;
}


Main.cpp;
1
2
3
4
5
6
7
8
#include <iostream>
#include "Header.h"

int main()
{
    std::cout << getFunctionCount() << '\n' ;
    std::cout << function() << '\n' ;
}


You tell me what happens. Does #pragma once allow the program to compile and link without error? What happens when you make function "inline" ?

External Linkage means it can be accessed from other translation units and internal means it can only be accessed in one?

Is that correct?

Yes.

[edit: Also, is there any way you could be persuaded to put something more descriptive in your subject lines than "Couple questions!" "Two questions!" "What is the difference!" "What does this mean!" "Why wouldn't this work???" to actually reflect on the subject matter of the post? For instance, you might have titled this one "Inline functions and cpp files"]
Last edited on
I wrote:
Another quiz:
1
2
3
class Foo {
  void magic( Bar & );
};

So, what does the compiler need to know about type Bar in this code?

The answer is: the above compiles, if one adds just one line before it:
class Bar;
The type of the parameter of a member function does not affect the size of class Foo. Furthermore, it is just a reference paramenter. Similarly, the size of a member pointer variable is just the size of a pointer.

Now, if I would use words "pimpl idiom", somebody might ask yet more questions rather than resorting to websearch, which is just as capable of producing plenty of text to read.
Ok I will change the names of my titles to be more descriptive xD.

Thanks I understand it now.

Thanks kesiverto and cire!!!
Last edited on
So inline really only exists to be able to include it in multiple translation units, without it violating ODR, thanks!
Last edited on
Cant you just use a forward declaration and put the definition in a cpp file?

Would that work just as fine?
Yes.. then you aren't placing the definition in multiple translation units.
Thanks cire!!!

I can finally continue with my book with confidence, I was terribly confused on this lol.
Topic archived. No new replies allowed.
Pages: 12