struct example
{
int ex_value_1 = 435;
int ex_value_2 = 325;
}
and i have an instance of that struct,
is there then a way to check whether / compare if the instanced struct's member variables are different from the default structs members?
So something like this (except this doesn't work)
1 2
if(my_struct_instance.ex_value_1 == example::ex_value_1)
cout << "ex value 1 has not been changed\n";
Thanks.
But unfortunatly my struct has quite alot of members variables
and i actually have multiple of these big structs that i need to check.
I also need to check just a single member at a time not every member, so i would have to make a isDefault function for every single member variable.
So im not so sure this solution will work for me :\
// Example program
#include <iostream>
#include <string>
struct example
{
int ex_value_1 = ex_value_1_default;
int ex_value_2 = ex_value_2_default;
staticconstint ex_value_1_default = 435;
staticconstint ex_value_2_default = 325;
};
int main()
{
example my_struct_instance;
my_struct_instance.ex_value_2 = 42;
if (my_struct_instance.ex_value_1 == example::ex_value_1_default)
std::cout << "ex value 1 has not been changed\n";
if (my_struct_instance.ex_value_2 == example::ex_value_2_default)
std::cout << "ex value 2 has not been changed\n";
}
_____________________________
Logically, if you want to compare to default values, you're going to have to store them somewhere. You can't avoid this.
#include <iostream>
usingnamespace std;
struct example
{
int a = 435;
int b = 325;
};
const example EXAMPLE; // One, constant, default structure
bool isChanged( example e, int example::*ptr ) { return e.*ptr != EXAMPLE.*ptr; }
int main()
{
example e1, e2;
int example::*ptr;
ptr = &example::a; // Choose which member to point to
for ( auto e : { e1, e2 } ) cout << boolalpha << isChanged( e, ptr ) << '\n';
cout << "----\n";
e1.a = 440;
for ( auto e : { e1, e2 } ) cout << boolalpha << isChanged( e, ptr ) << '\n';
}
Can you explain what int example::*ptr; means?
pointer to int... but restricted to only being able to point to an int member of example? Didn't know such a restriction was possible in C++.
Look up "pointer to member function". It's a kind of "offset" pointer that can only be called off of an object of the correct type. In lastchance's case, it's a pointer to a member variable, but it's the same idea. He accesses the member variables through the global EXAMPLE object.
Ganado> your way of doing is better since it's shorter and clearer.
The int blabla::*p only means a pointer to a member variable that is an int. So if you have a lot more type in your class, that'll be a hell to implement. But, macros existed for sure :oD.
The use of a defauft constant object though is very interesting in lastchance's solution.
Combining both yours (Ganado) and lastchance, the solution is really good.
1 2 3 4 5 6 7 8 9 10 11 12
struct example
{
int ex_value_1 = 45;
int ex_value_2 = 111;
};
const example EXAMPLE;
...
if(my_struct_instance.ex_value_1 == EXAMPLE::ex_value_1)
std::cout << "ex value 1 has not been changed\n";
stav> I really don't like struct... to me it's the kind of "class" for lazy people. And here is an example of that "laziness".
If you use a non-defaut constructor to define your object, then you can explicitely mark a change in default value... if this was what you want ?
Or do you just want to check that the current value is not the default one ?