When accessing an object by reference, you have to use the .
operator to reach its members:
void Some_Func(TheStruct &My_struct) {
My_struct.member = 4;
}
int main() {
TheStruct My_struct;
Some_Func(My_Struct);
// or:
// TheStruct *My_struct = new My_struct;
// Some_Func(*My_Struct);
// delete My_struct;
}
When accessing an object by pointer, you have to use the ->
or *.
operator to reach its members:
void Some_Func(TheStruct *My_struct) {
My_struct->member = 4;
// or (*My_struct).member = 4;
}
int main() {
TheStruct My_struct;
Some_Func(&My_Struct);
// or:
// TheStruct *My_struct = new TheStruct;
// Some_Func(My_Struct);
// delete My_struct;
}
If you don't want to use the &
operator to get the address of an object variable (maybe because it overrides operator&
, for instance), you can use std::addressof()
in C++11 and later:
int main() {
TheStruct My_struct;
Some_Func(std::addressof(My_Struct));
}
Or use boost::addressof
:
#include <boost/utility.hpp>
int main() {
TheStruct My_struct;
Some_Func(boost::addressof(My_Struct));
}
Or implement addressof()
manually:
template<class T>
T* my_addressof(T& arg)
{
return reinterpret_cast<T*>(
&const_cast<char&>(
reinterpret_cast<const volatile char&>(arg)
)
);
}
int main() {
TheStruct My_struct;
Some_Func(my_addressof(My_Struct));
}