I have the following code:
#include <iostream>
#include <string>
using namespace std;
struct foo_s {
string a;
string b;
string c;
};
void print_field(foo_s* foo, string foo_s::* field) {
cout << "field: " << field << " - " << foo->*field << endl;
}
int main() {
foo_s my_foo = {
"a",
"b",
"c",
};
print_field(&my_foo, &foo_s::a);
print_field(&my_foo, &foo_s::b);
print_field(&my_foo, &foo_s::c);
return 0;
}
Its output is:
field: 1 - a
field: 1 - b
field: 1 - c
I'm having a bit of trouble understanding the specifics of what's going on in the print_field()
function. Namely:
field
? I imagine it's pointer-to-string-foo_s-member
field
always the same (1 in this case), yet foo->*field
yields different results?Mainly, I'm baffled at #2. I imagined field would be an "offset" from the start of the struct and foo->*field
would have been conceptually equivalent to something like
char* ptr = static_cast<char*>(foo);
ptrdiff_t offset = somehow_get_the_byte_offset_from_pointer_to_member(field);
ptr = ptr[offset];
string result = *static_cast<string*>(ptr);
but that seems to be out since field
's value doesn't vary across calls. What am I missing? How exactly is this specific operation described by the standard?
To get the value pointed to by a pointer, you need to use the dereferencing operator * (e.g., if pNumber is a int pointer, *pNumber returns the value pointed to by pNumber .
Like every other data type, structure variables are stored in memory, and we can use pointers to store their addresses. Structure pointer points to the address of the structure variable in the memory block to which it points. This pointer can be used to access and change the value of structure members.
In C, all arguments are passed to functions by value, including structs. For small structs, this is a good thing as it means there is no overhead from accessing the data through a pointer.
3 answers. It is not possible to set a struct with NULL as it is declared. Fila f = NUll; error: invalid initializer. So cast% from% to NULL , which is not even a type in this code, or assign Fila to a primitive type variable is "wrong".
There's no overload for <<
to format the value of a member pointer, so you won't get anything particularly useful if you try. There is an overload for bool
, and member pointers are convertible to bool
, so that is what happens here. The pointer isn't null, so it converts to true
, which by default is formatted as 1
.
To demonstrate further, you could try streaming boolalpha
first; then you should see true
rather than 1
.
The type of field
is, as you say, a pointer to a member of foo_s
of type std::string
.
The value of field
is 1
in all of these cases, because pointers to member are convertible to bool
, so when you output them, you get a 1
because they are not null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With