What is the difference between (*ptr).f() and ptr->f()? [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ - Difference between (*). and ->?
What is the difference between this:
(*ptr).f();
and this:
ptr->f();
in c++ where the ptr is a pointer to C++ class which has a function f?

If ptr is a normal pointer, then both are equivalent. ptr->f is a short-cut to dereference the pointer (equivalent to (*ptr)) and access the member of the dereferenced object (equivalent to .f).
If ptr is a class that overloads operator-> and operator*, then they will each call different operator overloads, and so could have different behaviour.

There's no difference at all. (*ptr).f(); is the uglier way to do this.
Actually, if ptr is some smart pointer and its operator* and operator-> are overloaded and execute some side-effects, then you may have a problem with this. But this is really, really bad thing to do. It's as evil as #define true false

Aside from stylistic/typing differences, there is no difference. It's exactly the same as (*ptr).member = 7; vs ptr->member = 7; when using a pointer to a structure or class.

Related

what does it do *((T**)m_ptr)? [duplicate]

This question already has answers here:
Why cast a pointer to a float into a pointer to a long, then dereference?
(5 answers)
In C, if I cast & dereference a pointer, does it matter which one I do first?
(6 answers)
Closed 4 years ago.
I see this in the code and can't understand what is going here:
T * ptr; // we have some pointer and it has proper adress
...
// Later I see this and I can't understand what is going here
ptr = *((T **)ptr);
Also, later in the code I see *((T**)ptr) = m_address;
What for this construction is used ?
*((T**)ptr)
Thanks!
It means that the author wished they'd written T** ptr instead, and are hacking around the fact that they didn't, by casting the pointer to a different type than that which it was declared with, before dereferencing it. It pretends that ptr points to T** instead of a T*.
There are some occasions on which this type punning is okay (e.g. it's commonly used with the struct sockaddr type to sort of implement polymorphism), but type punning T* to T** is very strange.
In fact, unless T is char, or T has a T* as its first member and there's no padding, it's also a code smell (and, as far as I'm aware, UB).
Avoid.

Difference between Pointer->Call() and (*Pointer).Call() [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ptr->hello(); /* VERSUS */ (*ptr).hello();
I am learning C++ and my question is if there is any difference between using the arrow operator (->) or dereferencing the pointer * for calling a function.
These two cases illustrate my question.
Class* pointer = new Class();
(*pointer).Function(); // case one
pointer->Function(); // case two
What is the difference?
If the operators * and -> aren't overloaded, both versions accomplish the same.
Given
Class* pointer = new Class();
Then
(*pointer).Function(); // case one
dereferences the pointer, and calls the member function Function on the referred to object. It does not use any overloaded operator. Operators can't be overloaded on raw pointer or built-in type arguments.
pointer->Function(); // case two
This does the same as the first one, using the built-in -> because pointer is a raw pointer, but this syntax is better suited for longer chains of dereferencing.
Consider e.g.
(*(*(*p).pSomething).pSomethingElse).foo()
versus
p->pSomething->pSomethingElse->foo()
The -> notation is also more obvious at a glance.

C++ :: double colon marks and (void *) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does (char *) x or (void *) z mean?
I am working with a c++ file and have encountered the following line:
tmp.sort(Hash::pairval, printPair, (void *)(tmp.bitSize()));
I am most unsure of what (void *) means. bitsize() is a function, and I have heard the term passing a function pointer before. Is that what this is?
I know the :: is normally the scope resolution operator, which I have seen in .cpp/.h object type files. I believe it is serving the same purpose here, to state that pairval is found in Hash.
Thanks
The (void *) is simply casting the return value of tmp.bitSize() to a void pointer type. Casting is a very common operation in C++ and c as well.
Hash::pair
Is most probably a call to a static member of class Hash.
The (void*) part is a cast to void pointer of tmp.bitSize() which most probably returns some kind of value. So there is no function pointer.
I am most unsure of what (void *) means. bitsize() is a function, and I have heard the term passing a function pointer before. Is that what this is?
Nope. Note the parentheses, tmp.bitSize() is a function call expression that is called and returns a value. Hence - no function pointers involved here.
The return value is then cast to the pointer-to-void type (i.e. the catch-all "pointer to something" type) in order to be passed to a function which expects such pointer.
Why on Earth would someone convert a bit size (which looks like a number) into a pointer, I have no idea. This is somewhere between dubious and incorrect.
Read up on casting in C++. C-style casts are discouraged and casting to void* is seldomly useful and often dangerous because of the strict aliasing rule.
know the :: is normally the scope resolution operator, which I have seen in .cpp/.h object type files. I believe it is serving the same purpose here, to state that pairval is found in Hash.
That's correct.

What's the difference between fun(...) and (*fun)(...) using a function pointer in C/C++ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How come pointer to a function be called without dereferencing?
How does dereferencing of a function pointer happen?
Supposing I have a function pointer like:
void fun() { /* ... */ };
typedef void (* func_t)();
func_t fp = fun;
Then I can invoke it by:
fp();
or
(*fp)();
What is the difference/
Precisely two parentheses and an asterisk.
Both call the function pointed to by fun, and both do so in the same manner.
However, visually, (*fun) makes it clear that fun is not a function in and of itself, and the dereference operator is a visual cue that it is a pointer of some kind.
The without-parentheses syntax, fun(), is the same as a regular function call and so visually equates to that, making it primarily clear you're calling some kind of function. It takes context or a lookup to notice that it is a function pointer.
This is just a style difference, as far as what happens.

const_casting question [duplicate]

This question already has answers here:
Two different values at the same memory address
(7 answers)
Closed 5 years ago.
I have the following code:
int main(){
const int a = 1;
const int* b(&a);
int* c = const_cast<int*>(b);
*c = 29;
cout<<*c<<a<<*b;
return EXIT_SUCCESS;
}
Why doesnt the value of 'a' change to 29? Does this mean that the constness of a is not removed when const_casting b?
Constant variables also allows the compiler certain optimizations, one of these is that the compiler can keep the value in the registers and not reload it. This improves performance but will not work with variables that changes since these need to be reread. Some compilers even optimize constants by not allocating a variable, but simply replacing the value inline. If you change the variable a to int instead of const int it will work, as it can be read in the documentation about the const_cast operator from IBM:
If you cast away the constness of an
object that has been explicitly
declared as const, and attempt to
modify it, the results are undefined.
You can find more information about the problem you are having and why it doesn't work here:
The const_cast operator (IBM)
C++ const_cast usage instead of
C-style
casts
const_cast
confusion
On a side note it can be noted that if you find yourself in need of using the const_cast there is a good chance that you should reconsider your design instead.