Cumulated access to struct members
-
Is it possible in c++ to access struct members from a pointer without dereferencing the pointer everytime an element is requested (something like the with keyword from pascal)?
typedef struct { double x; double y; } Pnt; int main(void){ Pnt *PntPtr; with Pnt{ x=1; y=2; } }
instead of
typedef struct { double x; double y; } Pnt; int main(void){ Pnt *PntPtr; PntPtr->x=1; PntPtr->y=2; } }
-
Not exactly. It's not an extremly useful feature anyway.
struct foo { double x, y; }; foo* get_foo() { /* expensive, don't call multiple times */ } int main(){ { foo& a = *get_foo(); // local scope justifies short identifier a.x=1; a.y=2; } }
is probably the best approximation.
-
No. Use References if you want a more comfortable syntax than pointers (and you do not need or do not want the possibility of null-pointers). Give your classes useful modifiers instead of doing everything by hand.
class Point { double x, y; public: Point(double x, double y): x(x), y(y) {} void setxy(double xx, double yy) { x = xx; y = yy; } friend std::ostream& operator<< (std::ostream& out, const Point& point) { return out << point.x << '\t' << point.y; } }; void change(Point &point) { point.setxy(6,7); } int main() { Point p(1,2); std::cout << p << '\n'; change(p); std::cout << p << '\n'; }
-
Ok, made a small test program comparing the different methods.
Seems, in speed there is not much difference (about 10%), compiled with gcc and running on a win7 intel machine.Therefore it seems it is the best way either to use all references or all pointers, dependent if you write a cpp module for c or cpp itself.
#include <iostream> #include <ctime> using namespace std; struct Point_t {double x,y,z;}; double distance1(Point_t* p){ return p->x*p->x + p->y*p->y + p->z*p->z; } double distance2(Point_t* p){ Point_t &q = *p; return q.x*q.x + q.y*q.y + q.z*q.z; } double distance3(Point_t &p){ return p.x*p.x + p.y*p.y + p.z*p.z; } int main(void){ Point_t p = {1,2,3}; clock_t c; c = clock(); cout << "Testing Struct Access with dereferencing" << endl; for (long long int i = 0; i < 1000000000; i++){ //cout << distance1(&p) <<'\t'; distance1(&p); } cout << "clocks needed:" << (clock()-c)/(CLOCKS_PER_SEC) << endl; c = clock(); cout << endl << "Testing Struct Access, converting pointer to reference" << endl; for (long long int i = 0; i < 1000000000; i++){ //cout << distance2(&p) << '\t'; distance2(&p); } cout << "clocks needed:" << (clock()-c)/(CLOCKS_PER_SEC) << endl; c = clock(); cout << endl << "Testing Struct Access, only reference used" << endl; for (long long int i = 0; i < 1000000000; i++){ //cout << distance2(&p) << '\t'; distance3(p); } cout << "clocks needed:" << (clock()-c)/(CLOCKS_PER_SEC) << endl; }
-
lol?
If you look at the generated assembler code, you will notice that it's the same in all three cases.
-
MC schrieb:
Ok, made a small test program comparing the different methods.
Seems, in speed there is not much difference (about 10%), compiled with gcc and running on a win7 intel machine.Actually, there should be absolutely no difference at all. Did you activate optimization? Probably not, as the GCC is a rather aggressive optimizer and would have completely removed all the loops as they do not actually do anything.
-
He´s resetting
c
toclock()
before writing to the console, so he´s including the time needed to display messages in his measurement results. This may lead to somewhat inaccurate results.
-
SeppJ schrieb:
Actually, there should be absolutely no difference at all. Did you activate optimization? Probably not, as the GCC is a rather aggressive optimizer and would have completely removed all the loops as they do not actually do anything.