Skip to content

Instantly share code, notes, and snippets.

@laparca
Forked from anonymous/Makefile
Created April 24, 2014 09:22
Show Gist options
  • Save laparca/11247896 to your computer and use it in GitHub Desktop.
Save laparca/11247896 to your computer and use it in GitHub Desktop.
This exaplample allways fails because the compiler remove one of the constructors. This has sense because the structs have the same name.
#include <iostream>
struct MyStruct {
MyStruct() : x(5) { std::cout << "a.cpp: Calling MyStruct::MyStruct()" << std::endl; }
int x;
};
void run_example_a() {
MyStruct ms;
std::cout << "run_a: sizeof(MyStruct) = " << sizeof(MyStruct) << std::endl;
std::cout << "run_a: ms.x = " << ms.x << std::endl;
}
#include <iostream>
struct MyStruct {
MyStruct() : x(8), y(x) { std::cout << "b.cpp: Calling MyStruct::MyStruct()" << std::endl; }
int x;
int& y;
};
void run_example_b() {
MyStruct ms;
std::cout << "run_b: sizeof(MyStruct) = " << sizeof(MyStruct) << std::endl;
std::cout << "run_b: ms.x = " << ms.x << std::endl;
std::cout << "run_b: ms.y = " << ms.y << std::endl;
}
void run_example_a();
void run_example_b();
int main() {
run_example_a();
run_example_b(); // Fails with SIGSEGV
return 0;
}
OBJS=main.o a.o b.o
all: test_duplicated_structs
clean:
rm test_duplicated_struct $(OBJS) || true
test_duplicated_struct: $(OBJS)
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment