473,472 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Advice on wrapping data

I have data concerning h/w capabilities.
I intend to use a header file to do #defines, and put it into a class CHardware
along with serialisation function for the variables assigned values from the .h.
So, I get code like...

CHardware hardware;
hardware.serialise();
etc

which isn't a problem.

However, functions in other clesses need to have access to this data as well.
The obvious way is to use the .h file with it's #defines. However, I would
rather the other classes accessed it from a global instance of CHardware. Is
this possible or advisable?

--
Dirk

The Consensus:-
The political party for the new millenium
http://www.theconsensus.org
Jan 2 '06 #1
3 1051
Argh, I composed a whole nice reply for you and google ate it! Okay,
you get the short version without nifty code snippets. In fact, you
get bullet points.

* Don't use #define for constant data. Use const variables. In cases
like this, where the data is associated with a class, use static const
variables so that the data is associated with the class itself. You
don't need a global instance, just ClassName::ConstantName.
* Check out the Boost serialization library and understand what it can
do for you before you go off inventing your own. Boost's
implementation is usually a better way to go than a homebrew.

Luke

Jan 2 '06 #2
As Mr. Meyers pointed out, don't use #define for constant data.

Consider this approach:
foo.H

class foo
{
public:
int getCharacteristicA() const;
int getCharacteristicB() const;
int getCharacteristicC() const;
};
foo.C

static const int valueA = 0;
static const int valueB = 1;
static const int valueC = 2;
int foo::getCharacteristicA() const { return valueA; }

int foo::getCharacteristicB() const { return valueB; }

int foo::getCharacteristicC() const { return valueC; }
This way client code of the class gets the public interface, and has to
go through your instance to get the values. The actual values are out
of the way, static file-scope to just foo.C.
Jan 3 '06 #3
Getters are a bit excessive for retrieval of constants. It's just
extra boilerplate, more code to maintain (or forget to maintain), and
more work for the compiler to optimize out. But if you must, they
should at least be static inline. Since statics would be initialized
in the cpp anyway, you maintain the benefit of not recompiling user
code if the values change.

Luke

Jan 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
2
by: Dan V. | last post by:
Why does the banner image wrap around when I choose a small window size? http://officeactivate.com/schuit/index.shtml I am 1/2 way done a css book and am using this code from a CSS generator....
24
by: sundew | last post by:
Hi all, I am developing an open source DHTML project named Wednus Window. http://wednus.com Wednus Window is a DHTML Web-Application Windowing System. It shell websites/web-applications with...
2
by: Andrzej Kaczmarczyk | last post by:
Hi I am experiencing something weird. maybe you could help me. I have two ineditable classes from outsource libraries: DataColumn and GridColumn I have built a wrapper class around...
9
by: Siv | last post by:
Hi, I am trying to find a wrapping routine for text and not having much luck. The reason I need one is that I am producing a report where the text that was stored in the database was written into...
6
by: Markus Ernst | last post by:
Hi Searching for a possibility to display some text with preserved white space and line breaks, but with long lines being wrapped, I found this CSS declaration, which I found helpful: pre {...
13
by: master_programmer | last post by:
Advice to new programmers considering ASP.NET...... Dont do it !! I was forced to use the Visual Studio to make ASP.NET applications. It is very very slow. You are better off forgetting the...
0
by: bp_jobemail | last post by:
I'm trying to use PHP to wrap the output of an HTML form before it goes into a precompiled C cgi script. Essentially, the company that I work for uses a purchased precompiled c program for their...
6
by: TheTamdino | last post by:
I have experienced this issue in both IE and in Firefox, but usually not the same page at the same time. I'll have text that begins and ends with the paragraph tag <p>. This tag is suppose to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
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...
1
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...
0
tracyyun
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...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.