C++ function call question - c++

i have a question about the function call in the following example:
int main()
{
int a, b;
cin >> a >> b >> endl;
cout << *f(a,b);
return 0;
}
So is *f(a,b) a valid function call?
Edit:: sorry for the errors, i fixed them now i'm a little tired

The code at least could be reasonable. For it to work, f must be defined as a function that returns either of two sorts of things: either it returns a pointer, in which case the * dereferences the pointer, so whatever it was pointing at gets sent to standard output. Otherwise, f must return some user-defined type that defines operator * to return something that's compatible with cout.

Whatever f is, *f(a, b) attempts to apply the indirection operator to the result of f(a, b).
If f is a function pointer and you're trying to call it, while you could do this:
(*f)(a, b)
Just doing f(a, b) is simpler.

I don't think there is any way to tell without seeing the definition of f. C++ requires a lot of context to know what is going on.

It is very tough to tell if it is valid or not without knowing what 'f' is. But if it returns something that can be dereferenced it looks fine as long as that 'dereferenced' value can be printed (an overloaded operator <<) should exist for the type.

Related

What is the different between +[](){}; and (+[](){}); and why the expression is valid

As the title stated. The code is compiled using GNU c++2a
int main(){
(+[](){});
return 0;
}
Compiles fine.
However, the following code generates warning: value computed is not used [-Wunused-value]
int main(){
+[](){};
return 0;
}
Further question is: my understanding about the expression [](){} is, it returns an r-value object std::function<void()>. While, I don't know there is a unary operator +, when the + applies on any r-value, should it be a compile error generated? Or maybe because of the operator precedence, the expression is interpreted in another way?
{} is, it returns an r-value object std::function<void()>
No, it creates a lambda/closure which is its own kind of thing. There are cases when that is turned into a std::function, but what you're actually getting is much more similar to a functor (a class that implements operator()) than a std::function - which is a type-erased holder for things which can be called.
The + sign forces the closure to be turned into a function pointer (because that's the only thing thats "easy" to convert to which can have a unary + applied to it), which when wrapped in () "uses" the pointer value in a list context. Without that, you compute a function pointer but then discard it immediately. It's telling you that your + sign is silly.

Why doesn't cin have specifier format or ampersand like scanf?

Having
int a;
in C++, why doesn't
cin >> a;
need ampersand like
scanf("%d", &a);
in C ?
In C++, we have not only variables and pointers (as in C), but also references. On the calling side, passing a reference looks like passing a variable by value, but on the side of the function called, it actually receives not a copy, but a reference to the actual variable and hence can alter it. You can think of a reference like a pointer but without the need to de-reference.
std::istream& operator>> (std::istream&i, some_type&x);
declares an operator that takes a reference to a std::istream and another to some_type and returns the std::istream reference. With this declaration you can now
some_type x;
std::cin >> x; // calls above operator, pasing x by reference
Passing by reference and passing by pointer are very similar, and may actually produce identical code:
void f1(int*x) { *x=2; }
void f2(int&x) { x=2; }
int a;
f1(&a); // is equivalent
f2(a); // to this
The common wisdom in C++ is to pass variables that shall not be altered either by value (if they fit into a single register) or by const reference. Variables that may be altered by the function called can either be passed by (non-const) reference or by pointer, this is a matter of style: with the latter it is explicit at function call that the variable can be altered, while the former (passing by reference) requires one to look up the API of the function to find out whether one passes by value, reference, or const reference.
cin and i are passed by reference to operator >> given it the ability to read and write cin and i.
As C has no true reference passing, scanf("%d", &a); code needs to pass the address of a for scanf() to write a value to a.
C and C++ use fundamentally different methods when passing arguments of varying type.
In C, a single function like scanf(const char* pattern, ...) can accept many different types of parameters. That's why it needs a pattern parameter where you tell it the actual types it has received. If you pass anything unexpected, you have a runtime issue.
In C++, a single function name may be implemented repeatedly for different types of parameters. This is called overloading. Each overloaded implementation will know exactly what it expects, and the compiler will assure that it only gets that type of values. If there's no matching overload, you have a compile-time error.
Another difference is that in C++, cin >> i is actually a function call of operator>>. In C, function calls all look like functionName(argument)
You can use scanf in c++ , How ever still better to use cin. "cin>>"not "cin<<"
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
float x;
scanf("%f",&x);
printf("%.1f",x);
return 0;
}

Understanding output of this code

This is an excercise in my textbook. I need to find the output of this code.
#include<iostream>
using namespace std;
int main()
{
int x[]={10,20,30,40,50};
int *p,**q,*t;
p=x;
t=x+1;
q=&t;
cout<<*p<<","<<**q<<","<<*t++;
return 0;
}
The output is
10,30,20
Here I dont understand the declaration of **q, and also how its value comes out to be 30. I also noticed that changing the last statement to
cout<<*p<<","<<**q<<","<<*t;
changes the output to
10,20,20
Could somebody explain what goes on behind the scenes here? Thanks a lot in advance.
Here, q is a pointer to a pointer to int, and it was set to point to t. So *q is identical to t, and **q is *t. Which means the cout expression can be rewritten as:
cout<<*p<<","<<*t<<","<<*t++;
Here you can see that t is read and modified in different parts of the expression, and the standard says that the order in which these parts are executed is not specified. So t may be modified before or after (or even while) it is read. When this kind of thing (unsequenced read and write to a variable) happens, we get undefined behavior: Anything can happen as a result. A specific compiler may give a specific result on a specific computer, but there is no guarantee that you will always get this result.
So this exercise is invalid, and there is no point in trying to figure out why you saw a specific output.
On the other hand, the second line you attempted:
cout<<*p<<","<<**q<<","<<*t;
is perfectly valid, because it doesn't modify t anywhere.
p and t are both of the type pointer to int, q is of the type pointer to (pointer to int)
The * operator makes a pointer to a reference.
So *p is of the type int&, so is *t.
*q is of the type int*& (read reference to a pointer to int)
You want to print an int value here and must therefore use the * operator a second time.
So the **q is just making a pointer to a pointer to int to a reference to int
I forgot to mention it: The process is called dereferencing pointers.
Maybe the descirption on this side will give you a better insight:
http://www.cplusplus.com/doc/tutorial/pointers/
++ operator has higher precedence than <<
When program is executed this are events:
int x[]={10,20,30,40,50};
int *p,**q,*t;
p=x;
t=x+1;
q=&t;
cout<<*p<<","<<**q<<","<<*t++; //1st change value of t to t+1,
//but return old t in place ^
//then to output stream 'p'=10, then 'q'=new 't'=old 't'+1=30,
//then old 't'=20 which is returned by sufix ++ operator

C++: Pointer to Member Function within Struct

This is a follow up question to my previous post: C++: Initializing Struct and Setting Function Pointer
My new question is how do I call a pointer-to-member function within a struct? I have modified my previous code to:
float MyClass::tester(float v){
return 2.0f*v;
}
struct MyClass::Example{
float(Scene_7::*MyFunc)(float);
float DoSomething(float a){
return (MyFunc)(a); //ERROR, SEE BELOW FOR OUTPUT
}
};
I then set the function as follows, and output the result to the call:
struct Example e;
e.MyFunc = &MyClass::tester;
std::cerr << e.DoSomething(1.0f) << std::endl;
I get the following error: must use '.' or '->' to call pointer-to-member function...
The problem is I don't know how to do this. I am guessing I have to call something like this->*(myFunc)(a) within DoSomething but this references the struct. I have tried searching "this within struct pointer-to-member function" but have not been able to find anything. Any help or suggestions would be great. I feel like I am close but it is just a matter of syntax at this point.
The operator precedence of .* and ->* is, IMHO, broken. You need to wrap the operands in parentheses in order to call.
return (this->*MyFunc)(a);
Otherwise, the compiler thinks you're doing this->*(MyFunc(a)), which is obviously invalid.
Interestingly enough, (this->*MyFunc) as a standalone expression is also invalid. It needs to be called on the spot.
I cant tell if you have other errors, do to the lack of code, but you are calling a member function the wrong way, you call them like this:
return (obj->*MyFunc)(6);
I am not sure if you can use this in place of obj on the line above, since from the declaration, you need a pointer to an object of type Scene_7, so you will need to know a pointer of the correct type at that spot in the code..
Remember that . operator has higher precedence than the * operator. So when dealing with pointes and structs etc you will need to use paranthesis or -> operator which means that the pointer points to what.

Using the same instance of an object as the argument and as the calling object in calling a function

I wrote a small program for an assignment in an object-oriented C++ class in which I had to write a function which displayed the magnitude of a complex number. It works when written this way and I know how to write it without arguments in the parameter, but I want to know if there is a functional problem with the code (the calling object and the argument are the same object).
// displays the magnitude of a complex number
void Complex::magnitude(Complex c) const {
cout << "\nMagnitude of "; c.display(); cout << " = ";
cout.precision(3); cout.setf(std::ios::fixed, std::ios::floatfield);
cout << sqrt(pow(static_cast<double>(c.real), 2) +
(pow(static_cast<double>(c.imag), 2)) ) << "\n";
} // end function magnitude
Here's the redundant part with c1 as the calling object and argument:
c1.magnitude(c1);
I know how to write this code without c1 as an argument for magnitude() and that all I have to do is delete the argument and delete "c." from the function and it will still work the same. My question is simply Is this be a problem functionally when a program is written like this?
Your code makes syntactically no sense. Either use a member function taking no argument, or a free-standing function with argument (or both).
namespace My {
struct Complex {
void magnitude() const; // member function
...
};
void magnitude(Complex const&); // free-standing function
}
These can be used like this
My::Complex c1;
c1.magnitude();
magnitude(c1);
(However, using the name magnitude for a function not returning the magnitude is dangerous and therefore bad practice, better call it print_magnitude_to_stdout().)
This will not be a problem functionally, but logically if the function must take an object as an argument, you should make this function static.
In addition, since you are not modifying the incoming object, you should make the argument const. Finally, since copying the object may be more expensive than copying a reference, you may want to change the argument to be passed by reference.
static void Complex::magnitude(const Complex &c);
...
Complex myComplex(...);
Complex::magnitude(myComplex);
Functionally your code is OK, with one caveat: your function takes its argument by value, and therefore relies on the copy constructor. If the copy ctor is fine, your code is fine; otherwise, it may not be.
Stylistically, I find this code objectionable. It should either be a parameterless member function, or a one-argument static function (taking its argument by const reference).