static array in c++ forgets its size
- by Karel Bílek
In this small example, c++ forgets size of an array, passed to a constructor. I guess it is something simple, but I cannot see it.
In classes.h, there is this code:
#ifndef CLASSES_INC
#define CLASSES_INC
#include <iostream>
class static_class {
public:
static_class(int array[]) {
std::cout<<sizeof(array)/sizeof(int)<<"\n";
}
};
class my_class{
public:
static static_class s;
static int array[4];
};
#endif
In classes.cpp, there is this code:
#include "classes.h"
int my_class::array[4]={1, 2, 3, 4};
static_class my_class::s = static_class(my_class::array);
In main.cpp, there is only simple
#include "classes.h"
int main () {
return 0;
}
Now, the desired output (from the constructor of static_class) is 4. But what I get is 1. Why is that?