-
-
Save jyasskin/b6675d537dedb5d49c70 to your computer and use it in GitHub Desktop.
Demonstrate that classes default to external linkage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ clang++ test.cc test2.cc -o test && ./test | |
foo() == 1 | |
bar() == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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