Skip to content

Instantly share code, notes, and snippets.

@muendelezaji
Last active June 15, 2016 01:25
Show Gist options
  • Save muendelezaji/481dca031f11cea19913c50c28274531 to your computer and use it in GitHub Desktop.
Save muendelezaji/481dca031f11cea19913c50c28274531 to your computer and use it in GitHub Desktop.
Converting an array of struct pointers to a generic int pointer and recovering original structs from it. May or may not have been used to settle an argument or two ;)
// Coliru - http://coliru.stacked-crooked.com/a/655f184e31f1e779
#include <iostream>
using std::size_t;
using std::string;
using std::ostream;
using std::cout;
using std::endl;
using std::boolalpha;
using std::hex;
using std::dec;
struct object {
string name;
size_t age;
bool pass;
object() = default;
object(const string& name, const size_t age, const bool pass) : name(name), age(age), pass(pass) {};
object(const object&) = default;
object(object&&) = default;
object& operator=(const object &other)
{
object(other).swap(*this);
return *this;
}
object& operator=(object&& other)
{
object(std::move(other)).swap(*this);
return *this;
}
bool operator==(const object& other)
{
return name == other.name
&& age == other.age
&& pass == other.pass;
}
protected:
void swap(object& other)
{
std::swap(name, other.name);
std::swap(age, other.age);
std::swap(pass, other.pass);
}
friend ostream& operator<<(ostream& os, const object& obj)
{
return os << boolalpha << "obj{ name: " << obj.name << ", age: " << obj.age << ", pass: " << obj.pass << " }";
}
};
static constexpr auto SIZE = 5;
// void takes_struct_object_array(const object (&array)[SIZE]);
void takes_struct_object_array(const object array[]);
void takes_struct_pointer_array(const object* array[]);
void takes_integer_pointer(const size_t* array);
int main()
{
// const string x("hello world\n");
// cout << x << endl;
// 1.
object o11("o11", 11u, false),
o22("o22", 22u, true),
o33("o33", 33u, true),
o44("o44", 44u, false),
o55("o55", 55u, true);
cout << "individual objects:\n\t" << o11 << "\n\t" << o22 << "\n\t" << o33 << "\n\t" << o44 << "\n\t" << o55 << "\n" << endl;
// 2.
const object array[] = { o11, o22, o33, o44, o55 };
takes_struct_object_array(array);
// 3.
object *po11 = &o11, *po22 = &o22, *po33 = &o33, *po44 = &o44, *po55 = &o55;
const object* parray[] = { po11, po22, po33, po44, po55 };
takes_struct_pointer_array(parray);
// 4.
takes_integer_pointer((size_t*) parray);
}
// 2.
//void takes_struct_object_array(const object (&array)[SIZE])
void takes_struct_object_array(const object array[])
{
cout << "takes_struct_object_array(object array[])\n";
cout << "array of objects (structs):\n";
// for(auto obj : array) cout << obj << "\n";
for (size_t i = 0; i < SIZE; ++i) cout << "\t" << array[i] << "\n";
cout << endl;
}
// 3.
void takes_struct_pointer_array(const object* array[])
{
cout << "takes_struct_pointer_array(object* array[])\n";
cout << "array of pointers to objects:\n";
for (size_t i = 0; i < SIZE; ++i) cout << "\t" << *array[i] << "\n";
cout << endl;
}
// 4.
void takes_integer_pointer(const size_t* array)
{
cout << "takes_integer_pointer(size_t* array)\n";
cout << "pointer to integers:\nthe integers are really addresses of pointers to objects (need to dereference twice)\n";
for (size_t i = 0; i < SIZE; ++i)
{
cout << hex << "\t" << &array[i] << " => " << (object *)array[i] << " => " << *(object *)array[i] << ";\n";
}
cout << endl;
}
@muendelezaji
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment