0

I tested this in wandbox.org

Foo.h ::-

struct Foo {
    Foo();
    void dummy(){}
};
static Foo foo;

Foo.cpp ::-

#include "Foo.h"
#include <iostream>
Foo::Foo()  {
    std::cout<<&foo<<std::endl;
    std::cout<<this<<std::endl;
};

main.cpp ::-

#include "a.h"
int main(){
    foo.dummy();
}

Result ::-

0x7f333ff1077e
0x7f333ff1077e
0x7f333ff1077e
0x7f333ff1077c
  1. Why 2 thread local variables are created? (not 1? / is it actually 1?)
  2. Why the address of &foo and this are not the same?

If I pack all of code into 1 header, the result will be as expected. ( https://coliru.stacked-crooked.com/a/d9184eae09b0bffd )

0x7f385b59973f
0x7f385b59973f
cppBeginner
  • 1,114
  • 9
  • 27

0 Answers0