const MyStruct in a namespace

Hello i am trying to make my code more editable and more Object Oriented.

I want to have a const struct in my namespace called TileType.

It have two const Properties.

But i can't initialize it in a namespace of course. I dont know how to solve it even tho its very basic.

So any ideas?

EDIT: I know i can make a class and make functions that just returns constants but it feels like a ugly solution for an editable code. Is it possible another way?
Last edited on
I personally did not understand what you mean.

If you mean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//In the header.
namespace MyNS
{
    struct MyStruct { int SomeValue; double SomeOtherValue; };

    //global variable.
    extern struct MyStruct const g_MyConst;
}

//In a CPP file:
namespace MyNS
{
   struct MyStruct const g_MyConst = { 1, 2.0 };
}


Then yes, you should be able to do it.

I personally did not understand what you mean.

If you mean


That is exactly what i meant, thank you for your help. i will see if it solves my problem in a hour, too busy right now
I don't even think he has to go that far - this should do it.
1
2
3
4
5
6
7
namespace MyNS
{
    struct MyStruct { int SomeValue; double SomeOtherValue; };

    //global variable.
    struct MyStruct const g_MyConst {1, 2.0 };
}
I dont think you can initialize things in a header file
constant objects can be initialised in header files

(although to be fair - the extern method as shown by WebJose might be better
as you might change your mind about the object being constant, and then end up getting problems with miltiple definitions when you remove the const keyword)
Last edited on
Hello, i still got some problems after this.

When i am trying to use the ID of TileType as a case expression i get a compiler error.

case expression not constant

I dont see why it wouldnt be constant.

I will post my code here now.

This is my global namespace in the header file.
1
2
3
4
5
6
7
8
9
10
11
12
namespace global
{
        struct TileType{
		const int ID;
		const int Properties;
		const unsigned int textureID;
	}; 

	extern struct TileType const TILE_TYPE_GRASS;
	extern struct TileType const TILE_TYPE_ROAD_DIRT;
	extern struct TileType const TILE_TYPE_TOWER_ARROW;
}


This is the global namespace in the cpp file.
1
2
3
4
5
6
namespace global
{
	struct global::TileType const global::TILE_TYPE_GRASS = {global::TileData::TILE_ID_GRASS, global::TileData::TILE_PROPERTIES_ENVIRONMENT, global::textures::TEXTURE_GRASS};
	struct global::TileType const global::TILE_TYPE_ROAD_DIRT = {global::TileData::TILE_ID_ROAD_DIRT, global::TileData::TILE_PROPERTIES_ROAD, global::textures::TEXTURE_ROAD_DIRT}; 
	struct global::TileType const global::TILE_TYPE_TOWER_ARROW = {global::TileData::TILE_ID_TOWER_ARROW, global::TileData::TILE_PROPERTIES_TOWER, global::textures::TEXTURE_TOWER_ARROW}; 
}


Please help me
You didn't show us the case expression that's not constant.
You didn't show us the case expression that's not constant.


Oh, excuse me i forgot!
here it is!

1
2
3
4
5
6
switch(type)
{
case global::TILE_TYPE_GRASS.ID: //The case expression
	CurrentColor = Colors::COLOR_TILE_GRASS;
	break;
}


Last edited on
There's constant and there is constant.
It might mean a trawl through the c++ standard doument of what constitutes a
compile time constant expreesion that is suitable.
Trying to do it the way you are doing has never been acceptable.

However C++11 intoduces the constexpr keyword which we use
to gaurantee to the compiler that the value we are using is a genuine constant.
This will work with what you are trying to do.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class xyza
{
public:
    const int yy;
};




int main()
{
    
 constexpr  xyza qqq{23}; //use constexpr keyword instead of const keyword.   
    
    int abc(3);
    switch (abc)
    {
        
        case   qqq.yy: //now we can use a member name
        
        break;
        
        default:
        break;
    }

}


You will however, need a C++11 compliant compiler.
Hmm, I do have a c++11 compiler. But i do not understand how this can help me when i am having a namespace and not a class.

Could you please explain it to me.

Thanks for your response!
I hope this makes it clearer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// header file - For example myheader.h
#ifndef my_header_h
#define my_header_h

namespace namespaceX
{
    class XYZA
    {
        public:
            const int yy;
    };
    
    //constant expressions can be defined in header files
    //we can define our constant structures here
    //note that we use  constexpr rather than const keyword.
    constexpr  XYZA  qqq{23};
}

#endif



//some cpp file
#include "myheader.h"

namespace namespaceX
{
     

}


//main.cpp
#include "myheader.h"
int main()
{
    
    int abc(21); //test value
    
    switch (abc)
    {
        case    namespaceX::qqq.yy: //now we can use a member name
        cout << namespaceX::qqq.yy << endl;
        break;
        
        default:
        cout << "No match" << endl;
        break;
    }
}
   


EDIT:
For the record, the code as shown compiles and runs with mingw/gcc 4.7 with -std=c++11 compiler switch.
Last edited on
I just got home from work and i will try it in a few hours, my mind is bad right now. I will try this later, thank you for responding.

But i still dont understand why my expression wasnt constant, Could you please explain that. I hate using code im not totally sure why i use.
Last edited on
Hello, I have take a look at this solution with the constexpr keyword. I felt that i made my code unstructured and im now working on some kind of class.
Topic archived. No new replies allowed.