a
as a a1()
, it does not raise an error, but doesn't call the constructor. Why is this happening?My code:
#include<iostream>
using namespace std;
class a
{
public:
a()
{
cout << "in a\n";
}
};
int main()
{
a a1();
a a;
}
&n writes the address of n . The address of a variable points to the value of that variable.
In C, this is a normal parameter name, no different from any other name. You're probably using a C++ compiler, where this is a keyword and will give an error when used as a parameter.
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C - A.
C language uses 4 storage classes, namely: auto: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language.
When you write a a1();
it is actually being parsed as a function declaration not a call to the default constructor.
a a1;
will call the default constructor correctly
When you write a a;
it works because the variable name takes preference over the class name in what is called name hiding, but even though it works it will only lead to confusion and I would avoid doing it.
And for all those people who like standards quotes here you go
A class name (9.1) or enumeration name (7.2) can be hidden by the name of a variable, data member, function, or enumerator declared in the same scope. If a class or enumeration name and a variable, data member, function, or enumerator are declared in the same scope (in any order) with the same name, the class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is visible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With