MACROS - what can u do with them?

Can I use them to customise the header file???

I only want to include SomeStruct if I have defined MYARRAYSIZE

My Header File
1
2
3
4
5
#ifdef MYARRAYSIZE
struct SomeStruct {
int someArray[MYARRAYSIZE]
};
#endif 



Main.cpp (instance 1)
 
//nothing defined, struct won't be included and MYARRAYSIZE isnt required 



Main.cpp (instance 2)
1
2
#define MYARRAYSIZE 10
//MYARRAYSIZE is defined, struct is included and array is of size MYARRAYSIZE 


Is this proper?
Last edited on
This seems dangerous, because you're including a header with the declaration of SomeStruct, but you can only use SomeStruct if MYARRAYSIZE is defined. Very counter-intuitive.

Either use something like a template to declare the size of someArray if you really need a compile time size for the array:
1
2
3
4
5
6
7
8
9
10
11
template<int T>
struct Array
{
    Array():size(T){}
    int& GetI(size_t index){return i[index];}
    const size_t GetSize(){return size;}
private:
    int i[T];
    size_t size;
};
//Note: very unsafe code. There is no bounds checking whatsoever for index. 


Or just use the vector class:
http://www.cplusplus.com/reference/stl/vector/
Last edited on
@BlackSheep : there is already a class for this in the standard : std::array

http://www.cplusplus.com/reference/stl/array/
Topic archived. No new replies allowed.