What does this warning mean in orwell dev C++ IDE? [duplicate] - c++

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 8 years ago.
I recently wrote below simple program but compiler shows warning.
#include <iostream>
int main()
{
int a();
std::cout<<a;
return 0;
}
[Warning] the address of 'int a()' will always evaluate as 'true' [-Waddress]
What is the meaning of the above warning? Why value of a is 1 not 0?

It might look like a definition of a as an int, but:
int a();
declares a function a taking no parameters and return int.
Use:
int a{};
instead.
std::cout<<a;
calls operator<<() with bool which is always nonzero, hence true.

int a(); declares a function, not a variable. If you want a to be a zero-initialised variable, then you'll need one of
int a{}; // C++11 or later
int a = int();
int a(0);
int a = 0;
<< doesn't have an overload that can directly take a function; so it looks for a suitable conversion sequence to a type that it is overloaded for, and finds:
int() -> int(*)() -> bool
that is, using the standard function-to-pointer and pointer-to-boolean conversions. The function pointer won't be null, since a declared function must exist and have an address; so the boolean value will be true.

Related

Why passing std::move(object) and member of this object to function causes SIGSEGV [duplicate]

This question already has answers here:
Compilers and argument order of evaluation in C++
(6 answers)
Closed 5 years ago.
The following code results in SIGSEGV and I cannot understand why is that.
#include <iostream>
using namespace std;
struct C {
C(int x) { ptr = new int(x); }
C(C&& c) { ptr = c.ptr; c.ptr = nullptr; }
int* ptr;
};
void foo(int* x, C c) {
cout << *x << endl;
}
int main() {
C c(10);
foo(c.ptr, std::move(c));
return 0;
}
I would expect the pointer c.ptr to be passed by value to the function foo, however it behaves like its passed by reference.
Now, if I change the ordering of arguments: void foo(C c, int* x), than the problem disappears.
The other solution is to create a local copy of c.ptr before calling to x, and than passing that local copy to foo.
I would like to understand why can't i pass c.ptr by value in the sample code above.
It is passed by value, however:
foo(c.ptr, std::move(c));
It is unspecified in which order the parameters that are passed to a function call get evaluated.
Order of evaluation of the operands of almost all C++ operators
(including the order of evaluation of function arguments in a
function-call expression ... ) is unspecified.
"Unspecified" means that they can be evaluated in any order. The order can even be different each time you run the program. Your compiler chose to generate code that evaluates the 2nd parameter with the std::move first. As such, your move constructor moves the pointer out of the object, setting it to null. Then, c.ptr gets evaluated, passing the now-null pointer by value.
When you call foo(c.ptr, std::move(c)); you can't be sure rather c.ptr or std::move(c) will be evaluated first. The order arguments are evaluated in a function call is unspecified. It seems in your case that std::move(c) will be evaluated first, leaving c.ptr as nullptr. You then do cout << *x << endl which attempts to dereference *x, where x is nullptr.

C++ `int* class::*member = NULL` compiles, why? [duplicate]

This question already has answers here:
Pointer to class data member "::*"
(18 answers)
Closed 7 years ago.
I cannot figure out how this compiles. It seems like it should not, and if I use a value other than NULL in the constructor it will not.
#include <stdio.h>
class MyClass{
private:
int *first;
public:
MyClass();
};
MyClass::MyClass(){
int whatever = 42;
//int* MyClass::*first = &whatever;//This does not compile
int* MyClass::*first = NULL;//This compiles
}
int main(){
MyClass doSomething;
return 1;
}
It seems that generally the type Class::member = value syntax is used for static vars, which this is not.
Also, there is an asterisk before the member name, which confuses things even more.
If I switch the lines to the one that is commented out, the compiler complains, as expected.
error: cannot convert ‘int*’ to ‘int* MyClass::*’ in initialization
While I did expect an error, I have no idea what the type int* MyClass::* is. Or how it could be used.
This is a pointer to data member. You cannot initialize it with an ordinary pointer, that is why the commented out expression does not compile.

How to declare a string using the user input ( CIN ) in c++? [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 8 years ago.
What's the effect of int a(); in C++?
Is it equivalent to int a or int a(0)?
And how about char c() and double d()?
What's the effect of int a(); in C++?
That declares a function, with no parameters, that returns an integer.
Is it equivalent to int a or int a(0)?
No. Each of these declares a variable of integer type; the second also initialises it with the value zero.
And how about char c() and double d()? Thanks.
These also declare functions, with different return types.
int a(); is a function declaration.
int a is declaring a to be of type int.
int a(0) is declaring a to be of type int and initialising it to 0.
char c() and double d() are function declarations also returning char and double respectively.
All the function declarations should be terminated by a ;.
Neither, it declares a function.

what is the difference returning const or non-const in primative types c++ [duplicate]

This question already has answers here:
non-class rvalues always have cv-unqualified types
(2 answers)
Should I return const objects?
(12 answers)
What this const before method name mean?
(1 answer)
Closed 8 years ago.
I've tried to understand what differs if I add const or ignore it while returning function. Let me explain my question through an example.
const int foo()
{
return 3;
}
int main()
{
int check;
check=foo();
cout<<"before:"<<check<<endl;
check=1;
cout<<"after:"<<check<<endl;
return 0;
}
Up to now, I always think that, since I write const foo() I dont be able to change the check varaible,however I compiled it and get no error.
I wonder what I gain or loose by writing const before my foo() function.
Thanks in advance
A const modifier on primitive return types will be ignored.
See also this question: Should I return const objects?
You're not changing the variable. You're changing a copy of it.
check=foo();
assigns the value returned by foo to check. check is not const.
The difference is following compiler warning:
warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
const int foo()
^
See live demo.
This type of thing is ignored, so it has no effect.
It will make difference when you will try to return a reference.
For example:
int gGlobal;
const int & func()
{
return gGlobal;
}
int main ()
{
//Following statement will give error.
func() = 3;
return 0;
}

What's the effect of "int a(); " in C++? [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 8 years ago.
What's the effect of int a(); in C++?
Is it equivalent to int a or int a(0)?
And how about char c() and double d()?
What's the effect of int a(); in C++?
That declares a function, with no parameters, that returns an integer.
Is it equivalent to int a or int a(0)?
No. Each of these declares a variable of integer type; the second also initialises it with the value zero.
And how about char c() and double d()? Thanks.
These also declare functions, with different return types.
int a(); is a function declaration.
int a is declaring a to be of type int.
int a(0) is declaring a to be of type int and initialising it to 0.
char c() and double d() are function declarations also returning char and double respectively.
All the function declarations should be terminated by a ;.
Neither, it declares a function.