Please help me clear the concepts with following problems:
(executed and tested on linux,gcc)
Problem 1:
In the following simple example what exactly meant by A a()??
I found that this is not a definition of default constructor but a() is a function with return type is A.
If it is correct then why this code does not give me any linker error or runtime error.This code runs and links smoothly as if it knows the definition of function a().
class A
{
public:
void print()
{
printf("In class A\n");
}
};
int
main()
{
A a();
//a.fun(); //throws error "request for member ‘fun’ in ‘a’, which is of type ‘A()’"
}
problem 2.
In the following code the definition of array b throws error.
I am not able to find the exact reason for this behavior.
int a[]={3,4,21,5,7,86};
int b[a[3]]; //this throws error why???
int
main() { ... }
A a();This tells the compiler that a function a exists. Compiler says OK! But since it’s never USED, the linker never check its, and so there’s no linker error.int a[]={3,4,21,5,7,86};Unfortunately, the elements of array are not consideredcompile time constantsand thus cannot be used to initialize arrays nor template parameters. You’ll have to set the size of B another way, or do it dynamically at runtime:int *b = new int[a[3]];