I was just wondering why, when using object pointers, you must use the -> operatior. Why couldnīt you simply follow the standard method like: *p.funct() if you were accesing the funct() member through the *p pointer?
Thanks for the help
I was just wondering why, when using object pointers, you must use the -> operatior. Why couldnīt you simply follow the standard method like: *p.funct() if you were accesing the funct() member through the *p pointer?
Thanks for the help
sure, you can do that - but because of operator precedence you'll need to put a parenthesis around the pointer dereference, ie: (*p).funct()
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
And it's a lot easier to just write -> than to write all the parentheses and *'s, let alone site there and figure out where they're all supposed to go.
Hello,
For example sake, let's create a structure called point:The following declaration says that p is a pointer to a structure of type struct point. If p points to a point structure, *p is the structure, and (*p).x and (*p).y are the members:Code:struct point { int x; int y; };To use p, we might write, for example:Code:struct point *p;The parenthesis are necessary in (*p).x because the precedence of the structure member operator (.) is higher than (*). The expression *p.x means *(p).x, which is illegal here because (x) is not a pointer.Code:struct point *p; printf("(%d %d)\n", (*p).x, (*p).y);
Pointers to structures are so frequently used that an alternative notation is provided as a shorthand. If (p) is a pointer to a structure, then the following code refers to the particular member:
(p)->member-of-structure
Note: Both (.) and (->) associate from left to right.
Edit: Takes me forever to post. Heh.
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
Also, when you get into operator overloading, * and -> often will not mean "dereference" any more; rather, they will mean "dereference one of my member variables", in which case I personally find -> to be more intuitive - though I imagine that's totally up to you.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.