I have a public class Globals, which obviously holds all my global data.
I have an array of 243 items, currently structs of type
typedef struct STAR
{
int x;
int y;
int stellar_class;
}t_star;
t_star StarArray[243]={
{.....},
};
If I was doing this app in C I would just have the array as a seperate
C source file, compile it in, and have extern t_star StarArray[] where
ever I needed it.
What's the correct way to do this in cpp?
having global data is frowned upon, you can't put it in the class
header as this is against ansi conventions....
Are you supposed to define the empty array in the class header then
fill it in the class creation tool? What a waste of stack space....
puzzled...
-- 14 4950
* Stainless: I have a public class Globals, which obviously holds all my global data.
I have an array of 243 items, currently structs of type
typedef struct STAR { int x; int y; int stellar_class; }t_star;
1) Style: Don't use all uppercase except for macros, in C or C++.
2) Style: Don't use a typedef for a struct in C++ code.
Just do
struct t_star{ int x; int y; int stellar_class; };
t_star StarArray[243]={ {.....}, };
If this is meant to be a constant, use 'const'.
Instead of stating the number of elements, compute them.
t_star const StarArray[] = { ... };
size_t const StarArrayCount = sizeof(StarArra y)/sizeof(*StarArr ay);
If I was doing this app in C I would just have the array as a seperate C source file, compile it in, and have extern t_star StarArray[] where ever I needed it.
What's the correct way to do this in cpp?
There no "the" correct way.
A good way would be to do the same as you'd do in C.
having global data is frowned upon,
Having global _variables_ is frowned on, for good reasons.
you can't put it in the class header as this is against ansi conventions....
There's no such thing as class header (presumably you mean header file)
and there's no such thing as ansi conventions for C++ programming.
You can put data definitions in a header file.
The easiest way is to just declare the data 'const'; then it has
internal linkage.
If you want the data to be guaranteed to occur only once in the program
you can use a number of tricks.
One such trick is a function that returns a reference to the data.
Are you supposed to define the empty array in the class header then fill it in the class creation tool? What a waste of stack space....
There's no such thing as "class creation tool" in C++.
Formally there's not even such a thing as stack space, but for that it
is (in contrast to the other things) not difficult to understand what
you mean.
However, you're wrong about that.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Stainless wrote: I have a public class Globals, which obviously holds all my global data.
Don't make it a "public class". You're probably going to be better off
with a namespace.
I have an array of 243 items, currently structs of type
typedef struct STAR { int x; int y; int stellar_class; }t_star;
t_star StarArray[243]={ {.....}, };
If I was doing this app in C I would just have the array as a seperate C source file, compile it in, and have extern t_star StarArray[] where ever I needed it.
What's the correct way to do this in cpp?
There are several ways.
having global data is frowned upon, you can't put it in the class header as this is against ansi conventions....
Are you supposed to define the empty array in the class header then fill it in the class creation tool? What a waste of stack space....
No, you're supposed to make those members 'static' and define them outside
of the class definition, and initialise them there.
V
> I have a public class Globals, which obviously holds all my global data. I have an array of 243 items, currently structs of type
typedef struct STAR { int x; int y; int stellar_class; }t_star;
t_star StarArray[243]={ {.....}, };
If I was doing this app in C I would just have the array as a seperate C source file, compile it in, and have extern t_star StarArray[] where ever I needed it.
What's the correct way to do this in cpp?
Alright, I most likely do this the wrong way, but I like to use
standard container classes rather than arrays for things like structs.
The simpliest (not tested) example of this I can come up with:
Globals.h
-------------
class CGlobals{
public:
CGlobals::CGlob als();
CGlobals::~CGlo bals();
struct STAR
{
int x;
int y;
int stellar_class;
}t_star;
}
foo.h
-------
include "Globals.h"
class foo{
foo::foo();
foo::~foo();
std::list<CGlob als> StarArray;
}
foo.cpp
----------
static void main(){
CGlobals bar;
bar.t_star.x = 1;
bar.t_star.y = 2;
bar.t_star.stel lar_class = 4;
StarArray.push_ back(bar);
}
This way, you can have a dynamic array (list in this case), and still
use structs...
HOWEVER...if you're only using this struct and array in one class,
declare it as a private or protected member, and utilize it that way.
If you want the array global, make it a public member, and just include
that class header file whenever you want to use it.
An easier way would be to make a class with the three int's you want,
and then make a std::list of that class. This would eliminate the
struct altogether.
C++ is amazing and I hope I didn't confuse you at all. I do that too
myself sometimes.
Are you supposed to define the empty array in the class header then fill it in the class creation tool? What a waste of stack space....
What IDE are you using?
puzzled...
If you're working with C, you won't be puzzled for very long :).
-ben
> 1) Style: Don't use all uppercase except for macros, in C or C++. 2) Style: Don't use a typedef for a struct in C++ code.
Style varies depending on where you work, the typedef is the required
format for many places I have worked.
If this is meant to be a constant, use 'const'.
Not constant Instead of stating the number of elements, compute them.
t_star const StarArray[] = { ... }; size_t const StarArrayCount = sizeof(StarArra y)/sizeof(*StarArr ay);
I only put the size in to give people an idea of the amount of data I
had to store.
Having global variables is frowned on, for good reasons.
I reduced flash usage in a mobile phone by 1437 bytes, and ram usage by
538 bytes by making a single variable global.
I can see problems in multi-threaded environments, but a lot of "don't
do this" coding standards are designed to stop crap coders screwing
things up. Using global variables correctly can be a massive advantage
for a real world application.
There's no such thing as class header (presumably you mean header file) and there's no such thing as ansi conventions for C++ programming.
Well try initialising a variable in a class definition and see what
your compiler says .
Formally there's not even such a thing as stack space, but for that it is (in contrast to the other things) not difficult to understand what you mean.
However, you're wrong about that.
errr no, I have spent a lot of time hacking about with x86 code created
by c++ compilers and I know very well what crap they can generate.
C and C++ are stack based languages, variables and local storage all
come from the stack, the first thing you find in most subroutines is
mov ebp,esp
add esp,<some value>
then variable accesses are all
mov eax,[ebp+<offset]
mov [ebp+<offset],ebx
defining any local variables increases stack usage.
I don't know what would happen if I had a const array in the tool,
possibly the compiler could be smart enough to put that elsewhere, but
I would still have two copies of the entire array in memory somewhere.
The x,y,and stellar_class will be constant, but other booleans I will
be adding to the structure will be dynamic.
> No, you're supposed to make those members 'static' and define them outside of the class definition, and initialise them there.
V
This is one of those c++ static problems, when I use static inside a
function it means "retain value", but outside a class definition, on my
compiler at least it reduces visibility.
Defining the array as static makes it invisible to code outside the
current class implementation.
This would mean I have to add handlers for the array to the class
t_star getStar(int n);
void setFlag(int n, int flag);
etc.
which is just code bloat
--
I see what you mean, but you still end up manually filling the array.
I have seen a lot of people using lists in c++, but I am very suspicous
about how efficent the code will be.
I can see how the compiler will generate code for arrays of struct's
It will generate an array of pointers, the same way it does for
multi-dimensional arrays.
Accessing an element then becomes very efficent.
With lists I am not so sure what the compiler will generate, and
without time to experiment I am loath to use them.
While I am thinking about a clean solution, I am currently using a
frigg.
I have written a c tool that writes the array to disc, I then read this
into the array when the class is created.
Swapped disc space for memory in effect
Stainless wrote: No, you're supposed to make those members 'static' and define them outside of the class definition, and initialise them there.
V
This is one of those c++ static problems, when I use static inside a function it means "retain value", but outside a class definition, on my compiler at least it reduces visibility.
Defining the array as static makes it invisible to code outside the current class implementation.
This would mean I have to add handlers for the array to the class
t_star getStar(int n); void setFlag(int n, int flag);
etc.
which is just code bloat
Doesn't this work for you? I think it is what Victor suggested. Note
that he also suggested that you might just as well use a namespace
instead of a class full of only static data.
// Globals.h
struct t_star
{
int x;
int y;
int stellar_class;
};
class Globals
{
public:
static t_star StarArray[];
};
----------------------------------------------------------------
// Globals.cpp
t_star Globals::StarAr ray[243] = { ... }; // Initialise the array
----------------------------------------------------------------
#include "globals.h"
int main()
{
Globals::StarAr ray[0].x = 1;
int i = Globals::StarAr ray[0].stellar_class;
}
Gavin Deane
* Stainless: [confused]
Well.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: nt9 |
last post by:
I am very new with php so excuse me please if the question seems really
easy.I have a page(form) which displays a list of the titles of some
books(retreived from a database) and each title has a selection box with
option values 1 to 7 each.The user should be able to enter 7 choices of his
favourite books and store them in the database at a table called preferences
with fields: customer_id ,first_choice, sec_choice,third_choice etc.I am...
|
by: Mark Hannon |
last post by:
I am trying to wrap my brain around storing form elements inside
variables & arrays before I move on to a more complicated project. I
created this simple example to experiment and as far as I can tell, it
should work but it doesn't. Can someone tell me where I went wrong?
<html><head>
<title>Form Test</title>
<script language="JavaScript>
<!--
function copy(){
|
by: Wynand Winterbach |
last post by:
I think every C programmer can relate to the frustrations that malloc
allocated arrays bring. In particular, I've always found the fact that
the size of an array must be stored separately to be a nightmare.
There are of course many solutions, but they all end up forcing you to
abandon the array syntax in favour of macros or functions.
Now I have two questions - one is historical, and the other practical.
1.) Surely malloc (and...
|
by: Crirus |
last post by:
I have ~200 players on a server...
They share the same 512x512 tiles map...
Each player have an explored area and a visible area... visible area is
given by players's current units and buildings...
Now, I'm facing with the problem of how to store does areas...
One way is to keep a explored(512,512) array of booleans... each
explored(i,j)=True mean player explored the tile on (i.j)... the same for
visible...
|
by: Jacek Dziedzic |
last post by:
Hello!
Suppose I have a class that contains only public members
of builtin types and a default constructor. Because of the
default constructor it is no longer an aggregate and therefore
no longer POD, according to my understanding of
http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html
I need to be able to serialize this class to/from a binary file
| |
by: Martin Jørgensen |
last post by:
Hi,
I'm reading a number of double values from a file. It's a 2D-array:
1 2 3 4 5 6 7
-------------
1 3.2
2 0 2.1
3 9.3
4
|
by: assgar |
last post by:
Hi
Developemnt on win2003 server. Final server will be linux
Apache,Mysql and PHP is being used.
I use 2 scripts(form and process).
The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply.
Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money.
Note The above informaton is...
|
by: stephen b |
last post by:
(apologies for cross posting from the moderated group..i'm sure you
understand)
Hello, I'm passing an array into a Constructor and hoping to use it
as
a pointer and store it as a class member for future use. So far, I'm
just causing crashes, psuedo code below:
double block;
foo = MyClass(block);
|
by: deciacco |
last post by:
I'm writing a command line utility to move some files. I'm dealing with
thousands of files and I was wondering if anyone had any suggestions.
This is what I have currently:
$arrayVirtualFile =
array( 'filename'=>'filename',
'basename'=>'filename.ext',
'extension'=>'ext',
'size'=>0,
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |