Skip to content

Instantly share code, notes, and snippets.

@thiagoh
Last active March 25, 2017 07:56
Show Gist options
  • Save thiagoh/b716b1f24d15ec81f8b37d45d54e7e4f to your computer and use it in GitHub Desktop.
Save thiagoh/b716b1f24d15ec81f8b37d45d54e7e4f to your computer and use it in GitHub Desktop.
c++ good stuff
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <typeinfo>
#include <type_traits>
namespace com::test {
class BaseClass {
public:
BaseClass() {}
virtual ~BaseClass() {}
};
class MyClass : BaseClass {
public:
MyClass(int v) : myValue(v) {}
virtual ~MyClass() {}
private:
int myValue;
};
class MyExtendedClass : MyClass {
public:
MyExtendedClass(int v) : MyClass(v) {}
virtual ~MyExtendedClass() {}
};
}
// simulates an instanceof for c++
template<typename Base, typename Derived>
inline bool instanceof(const Derived*) {
return std::is_base_of<Base, Derived>::value;
}
using namespace std;
using namespace com::test;
int main() {
int x = 2;
const type_info& tfx = typeid(x);
auto y = 3;
const type_info& tfy = typeid(x);
auto z0 = new BaseClass();
const type_info& tfz0 = typeid(z0);
auto z1 = new MyClass(4);
const type_info& tfz1 = typeid(z1);
auto z2 = new MyExtendedClass(4);
const type_info& tfz2 = typeid(z2);
// intro tests
cout << "e: " << typeid(int).name() << endl;
cout << "x: " << tfx.name() << endl;
cout << "y: " << tfy.name() << endl;
cout << endl;
// printing the (ugly) names of our types
cout << "z0: " << tfz0.name() << endl;
cout << "z1: " << tfz1.name() << endl;
cout << "z2: " << tfz2.name() << endl;
cout << endl;
// typeid only compares the very same type... clearly doesn't work for our purpose
cout << "typeid(z0) == typeid(z1) " << ((tfz0) == (tfz1) ? "true" : "false") << endl;
cout << "typeid(z1) == typeid(z2) " << ((tfz1) == (tfz2) ? "true" : "false") << endl;
cout << "typeid(z0) == typeid(z2) " << ((tfz0) == (tfz2) ? "true" : "false") << endl;
cout << endl;
// now we're good to go!
cout << "instanceof<BaseClass>(z0) " << (instanceof<BaseClass>(z0) ? "true" : "false") << endl;
cout << "instanceof<BaseClass>(z1) " << (instanceof<BaseClass>(z1) ? "true" : "false") << endl;
cout << "instanceof<BaseClass>(z2) " << (instanceof<BaseClass>(z2) ? "true" : "false") << endl;
cout << endl;
cout << "instanceof<MyClass>(z0) " << (instanceof<MyClass>(z0) ? "true" : "false") << endl;
cout << "instanceof<MyClass>(z1) " << (instanceof<MyClass>(z1) ? "true" : "false") << endl;
cout << "instanceof<MyClass>(z2) " << (instanceof<MyClass>(z2) ? "true" : "false") << endl;
cout << endl;
cout << "instanceof<MyExtendedClass>(z0) " << (instanceof<MyExtendedClass>(z0) ? "true" : "false") << endl;
cout << "instanceof<MyExtendedClass>(z1) " << (instanceof<MyExtendedClass>(z1) ? "true" : "false") << endl;
cout << "instanceof<MyExtendedClass>(z2) " << (instanceof<MyExtendedClass>(z2) ? "true" : "false") << endl;
return 0;
}
/* offsetof example */
#include <stdio.h> /* printf */
#include <stddef.h> /* offsetof */
struct foo {
int z; // 4 bytes
char a; // 1 byte
char b; // 1 byte
char* c; // left spacer (until next eight byte window is full) + 8 bytes
char d; // 1 byte
char e; // 1 byte
char f[10]; // 10 bytes
char g; // 1 byte
};
int main ()
{
printf ("offsetof(struct foo,z) is %d\n",(int) offsetof(struct foo, z));
printf ("offsetof(struct foo,a) is %d\n",(int) offsetof(struct foo, a));
printf ("offsetof(struct foo,b) is %d\n",(int) offsetof(struct foo, b));
printf ("offsetof(struct foo,c) is %d\n",(int) offsetof(struct foo, c));
printf ("offsetof(struct foo,d) is %d\n",(int) offsetof(struct foo, d));
printf ("offsetof(struct foo,e) is %d\n",(int) offsetof(struct foo, e));
printf ("offsetof(struct foo,f) is %d\n",(int) offsetof(struct foo, f));
printf ("offsetof(struct foo,g) is %d\n",(int) offsetof(struct foo, g));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main() {
// int variable definition
int x = 1;
std::cout << "x: " << x << std::endl;
// y int variable copy of another variable
int y = x;
std::cout << "y: " << y << std::endl;
// z int variable reference another variable
int &z = x;
std::cout << "z: " << z << std::endl;
// w int variable gets the address of another variable
int *w = &x;
std::cout << "w: " << w << std::endl;
std::cout << "*w: " << *w << std::endl;
std::cout << std::endl;
printf("Values:\n");
printf("x = %d / y = %d / z = %d / *w = %d / w = %d \n", x, y, z, *w, w);
printf("Memory location:\n");
printf("## x %p\n", &x);
printf("## y %p\n", &y);
printf("## z %p\n", &z);
printf("## w %p\n", w);
x = 2;
y = 3;
printf("Values:\n");
printf("x = %d / y = %d / z = %d / *w = %d / w = %d // <-- different!\n", x, y, z, *w, w);
printf("Memory location:\n");
printf("## x %p\n", &x);
printf("## y %p\n", &y);
printf("## z %p\n", &z);
printf("## w %p\n", w);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <memory>
#include <string>
using namespace std;
static int UNIQUE_ID = 0;
class MyClassA {
private:
int id;
public:
MyClassA() : id(++UNIQUE_ID) {}
virtual ~MyClassA() {
printf("MyClassA (%d) destructed\n", id);
}
};
class MyClassB {
private:
int id;
public:
MyClassB() : id(++UNIQUE_ID) {}
virtual ~MyClassB() {
printf("MyClassB (%d) destructed\n", id);
}
};
unique_ptr<MyClassA> getUP_A() {
return unique_ptr<MyClassA>(new MyClassA());
}
unique_ptr<MyClassB> getUP_B() {
return unique_ptr<MyClassB>(new MyClassB());
}
MyClassA* getA() {
return new MyClassA();
}
MyClassB* getB() {
return new MyClassB();
}
int main() {
unique_ptr<MyClassA> a1 = getUP_A();
MyClassA* a2 = getA(); // will not be destructed
MyClassB* b1 = getB(); // will not be destructed
unique_ptr<MyClassB> b2 = getUP_B();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <memory>
#include <string>
using namespace std;
unique_ptr<string> formatS() {
int bufLength = 1024;
char* s = new char[bufLength];
snprintf(s, bufLength, "unknown token when expecting an expression: %d", 1);
return unique_ptr<string>(new string(s));
}
unique_ptr<char> formatC() {
int bufLength = 1024;
char* s = new char[bufLength];
snprintf(s, bufLength, "unknown token when expecting an expression: %d", 1);
return unique_ptr<char>(s);
}
int main() {
string s1 = "foo";
puts(s1.c_str());
puts("bar");
s1.append(",bar");
puts(s1.c_str());
unique_ptr<char> s2 = formatC();
puts(s2.get());
unique_ptr<string> s3 = formatS();
puts(s3.get()->c_str());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment