Skip to content

Instantly share code, notes, and snippets.

@jyasskin
Created May 28, 2014 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyasskin/b6675d537dedb5d49c70 to your computer and use it in GitHub Desktop.
Save jyasskin/b6675d537dedb5d49c70 to your computer and use it in GitHub Desktop.
Demonstrate that classes default to external linkage.
$ clang++ test.cc test2.cc -o test && ./test
foo() == 1
bar() == 1
#include <stdio.h>
int bar();
// Collides with another class defined in test2.cc.
class Test {
int member_;
public:
// I thought I'd need to make the constructor __attribute__((noinline)), but it fails this way too.
Test() : member_(1) {}
int get() { return member_; }
};
int foo() {
Test t;
return t.get();
}
int main() {
printf("foo() == %d\n", foo());
printf("bar() == %d\n", bar());
}
int bar();
// Collides with another class defined in test.cc.
class Test {
int member_;
public:
// I thought I'd need to make the constructor __attribute__((noinline)), but it fails this way too.
Test() : member_(42) {}
int get() { return member_; }
};
int bar() {
Test t;
return t.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment