static const data members definition
Hi you all,
I would like to know what is the best way for make the definition of the static const data members.
I would like to have two files:
- InputError.h
- InputError.cpp
I woule like to make the declaration of the class in .h and the definition in .cpp. Is this correct?
I have the following:
File InputError.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#pragma once
#define INPUT_ERRORS
class InputError
{
public:
static const int NumberZero;
static const int NumberOne;
static const int NumberTwo;
static const int NumberThree;
static const int NumberFour;
static const int NumberFive;
}
| |
File InputError.cpp:
1 2 3 4 5 6 7 8 9
|
#include "InputError.h"
static const int InputError::NumberZero=0;
static const int InputError::NumberOne=1;
static const int InputError::NumberTwo=2;
static const int InputError::NumberThree=3;
static const int InputError::NumberFour=4;
static const int InputError::NumberFive=5;
| |
Compiler fails, and I don't know if it is the "polite" way.
I want to use this class for making something like that in other part of the code:
if (condition) return InputError::NumberZero;
Any other idea is very welcome as well as tips!
Thanks in advance,
Pablo
You don't need to write 'static' in your cpp.
static const int members can (not sure if they must) be initialized in the class declaration.
Last edited on
@margareto
Like helios said the static const members can be inicialized in the class declaration.
Example:
1 2 3 4 5 6 7 8 9 10 11
|
//X.h
#ifndef X_H_INCLUDED
#define X_H_INCLUDED
class X {
public:
X::X(){}
X::~X(){}
static const int x = 10;
};
#endif // X_H_INCLUDED
| |
1 2 3 4 5 6 7 8 9 10 11 12 13
|
//main.cpp
#include <iostream>
#include <limits>
#include "X.h"
using namespace std;
int main()
{
X objectX;
cout << objectX.x;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
return 0;
}
| |
I hope this help you.
Thanks a lot! I did it just in a file. I hope it be "polite" anyway, since it is for my Ms Thesis and they want like that!
Thanks to all of you!
Pablo
Topic archived. No new replies allowed.