c++ questions (extern, unions)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.

c++ questions (extern, unions)

Postby serengeor » Tue Dec 27, 2011 10:07 pm

I'm currently again exploring the wonders of c/c++.
Q1)
I have come across the need to create a table of strings (char*).
The table should be declared in header file and defined in cpp to avoid multiple definitions.
What I did is I've put it like this in the header:
cpp Code: Select all
 
extern char * myCharMap[4];
 

In my cpp file:
cpp Code: Select all
 
char * myCharMap[4] = {"123","abcdef","ghjikl","mnoprstqrst"};
 

Now as far as I've tested, this works correctly and I avoid multiple definitions, Is this a correct way to go about this?
Q2)
I have been looking in irrlicht SEvent structure. It contains event type and an union that contains and structures for every event type.
Now by looking at those event structures I could say that they differ in size. Can unions contain structures with any kind of data in them? Does the data in structures have to be in some sort of order?
Working on game: Marrbles (Currently stopped).
User avatar
serengeor
 
Posts: 1701
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: c++ questions (extern, unions)

Postby Klunk » Tue Dec 27, 2011 10:40 pm

yes, it's a perfectly legit way to handle a string table.
Klunk
 
Posts: 232
Joined: Mon Jan 10, 2011 5:21 pm

Re: c++ questions (extern, unions)

Postby Mel » Tue Dec 27, 2011 10:51 pm

It is not the best practice though, but it is posible.

The advantage of unions is to access the same variable, but with diferent meanings. I am not sure about the size, (checked, no, there is no need for them to be of the same size) but the point is to access them as a struct, that instead of having 2 members in memory, has only 1, that can be addressed in diferent ways. The point is not to mix the accessing patterns, if you access a union, and initialize a union as a thing, access it as that same thing, as accessing it as the other type would lead to unexpected results.

For C++ reference, this page is really useful :)
http://www.cplusplus.com/doc/tutorial/other_data_types/
http://santiagong.daportfolio.com/
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
User avatar
Mel
Competition winner
 
Posts: 1788
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: c++ questions (extern, unions)

Postby serengeor » Wed Dec 28, 2011 11:13 am

Mel wrote:For C++ reference, this page is really useful :)
http://www.cplusplus.com/doc/tutorial/other_data_types/

I had used this page for ages, and I believe I have misread some things in the past about unions, that's why I wasn't sure of their usage.

Thank you both for your replies. :wink:
Working on game: Marrbles (Currently stopped).
User avatar
serengeor
 
Posts: 1701
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania


Return to Beginners Help

Who is online

Users browsing this forum: Google [Bot] and 1 guest