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

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.

Related

Assigning to value (rvalue ?) returned by function doesn't give compile error [duplicate]

This question already has answers here:
assigning to rvalue: why does this compile?
(1 answer)
rvalue on the left side
(2 answers)
Closed 4 years ago.
In the code below, why doesn't the assignment to get_string_val() give compilation error ? It looks like the function is returning an rvalue.
Of course, this is a simple example and this could be a member function returning a member variable. This can cause a bad bug if I had intended to return std::string& but mis-typed and returned std::string.
It looks like "xxx" is getting assigned to a temporary variable returned by get_string_val() ?
std::string get_string_val()
{
std::string x;
return x;
}
int main()
{
get_string_val() = "xxx";
}
That is because std::string has an overloaded (custom) assignment operator. This operator can be invoked for rvalue std::string, as long as the object is not const-qualified.
You'd have a compilation error if your get_string would return const std::string.
By the way, things work differently for built-in types, which do not have operator= overloaded for them. int get_int(); get_int() = 25; would be a compilation error.

C++ Is "const void" As Return Value More Const-Correct Than "void"? [duplicate]

This question already has answers here:
Should useless type qualifiers on return types be used, for clarity?
(6 answers)
Closed 8 years ago.
I have become somewhat of a const-correctness fanatic when it comes to programming. I've got const's everywhere (where correct of course). Now I've even started const'ing my void return types.
You can't create a void object and therefore you can't assign a value to a void object even if it's const or not, which means the "const" becomes redundant.
So am I const'ing my void return types for nothing?
I hope this isn't too philosophical for Stack Overflow.
TL;DR:
const void Foo( void );
vs
void Foo( void );
Is there any difference?
No, const void is completely meaningless. I'm surprised your compiler doesn't give you a warning, actually. Clang, for instance, told me:
example.cpp:1:1: warning: 'const' type qualifier on return type has no effect
[-Wignored-qualifiers]
const void Foo( void );
^~~~~~
1 warning generated.

is_const<const int&>::value is false -- why? [duplicate]

This question already has answers here:
Why does std::is_const<const int&>::value evaluate to false?
(2 answers)
Closed 7 years ago.
Why does this static assertion fire?
static_assert(std::is_const<const int&>::value, "Pain");
Would be awesome to get both a syntactic (why the implementation would do this) and a semantic reasoning (why they would have designed this type trait's interface to do this).
I am aware it is possible to throw in a std::remove_reference call to get the expected outcome, but I'm not sure why that's necessary.
const int& is a reference to const int. So the reference itself isn't const.
It's slightly confusing, so I'm going to present an analogy with const int*. It's the pointer to const int. But you can modify it
const int a = 5, b = 7;
const int* ptr = &a;
ptr = &b; // pointer is modified
so the pointer isn't const. (the const pointer would be int* const instead)

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

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.

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;
}