Do I have to declare store in my implementation file for all static
member variables, even when they are const ints?
In Windows, using msys with g++ 3.4.2 and whatever linker I'm not sure
(probably gnu binutils), I didn't have to declare storage for any of my
static const integer member variables, but in Linux, the linker can't
find four of them. I'm assuming the ones it found were simply replaced
by the compiler and it didn't need to keep them stored. Perhaps the
Windows g++ optimised the storage space away while my Linux g++ (3.3.6)
didn't.
Here is what I want to do:
class foo {
public:
static const int bar = 3;
};
Then I might reference bar somewhere down the line. Can I initialize bar
here in the header file? Do I still need to have
const int foo::bar;
in my implementation (cpp) file?
As I said, I didn't need that const int foo::bar line in Windows under
msys, but Linux isn't liking it. I'm assuming this is an optimization
thing, and I probably needed storage declarations for all my static
variables. Is this right?
Can I still initialize my variables in the class def even if I defined
them in the impl file?
My big problem, and the reason I didn't declare storage space in the
first place is I have something that looks like this:
class foo {
public:
static const int SRAM_FILE_SIZE = 0x2000;
private:
char buffer[SRAM_FILE_SIZE];
};
If I don't declare the value before buffer, it can't use the constant.
Thanks,
--John Ratliff 1 2130
According to Item #2 in Meyer's Effective C++, Third Edition,
static const integral types are allowed to be initialized at
the point of declaration (i.e. in the .h file).
In some cases compilers that support this may still require
a definition in the cpp file. For example, if you take the
address of the variable. From that item, it is clear that
if initialization is done in the .h file you can't do it in
the .cpp file. But it is not clear if either or is allowed.
Older compilers may not support this so you just need to
define and initialize the variable in the cpp file. For
these older compilers, where you need the const variable's
value further down in the class declaration (e.g. for an
array declaration) Meyer's describes the enum hack:
class A{
enum { Size = 100 };
int Array[Size];
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: BCC |
last post by:
Hi,
I have a class with several member variables that should be initialized
according to user input before an object is instantiated. Using a static
variable is required here.
But, I would like to have each object of my class have unique values of
these variables, because the values may be different depending on some other
factor in the object. Something like this:
|
by: Alexander Mahr |
last post by:
Dear Newsgroup,
I'm somehow confused with the usage of the static keyword.
I can see two function of the keyword static in conjunction with a data
member of a class.
1. The data member reffers in all objects of this class to the same data
Or in other word by using the static keyword all objects of one class
can share data. (This is what I want)
|
by: katekukku |
last post by:
HI,
Could anyone please tell me what are static variables and what exactly
are there features.
I am a little bit confused.
Thank You
|
by: Sahil Malik [MVP] |
last post by:
So here's a rather simple question.
Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there's web.config bla bla .. but lets just
say I wanna use global.asax) ---
Would you declare your static var as ---
public static int x ;
|
by: Colin Desmond |
last post by:
I have an MFC Extension DLL that I have flicked the /clr switch on and made a
few other tweaks so it compiles and runs in a managed MFC applicaiton.
The DLL contains a number of classes which contain static member variables
and static methods. Are these permitted in a managed DLL?
Thanks
Colin
| |
by: mangalalei |
last post by:
A static data member can be of the same class type as that of which it
is a member. A nonstatic data member is restricted to being declared as
a pointer or a reference to an object of its class.
And I haved used the sizeof operator to test a class which has a static
data member, the class size is all the nonstatic data member except the
static data member.
In compiler's view, is the static data member constructed after the
whole class...
|
by: mast2as |
last post by:
Hi guys
Here's the class I try to compile (see below). By itself when I have a
test.cc file for example that creates an object which is an instance of
the class SpectralProfile, it compiles fine.
1 / Now If I move the getSpectrumAtDistance( const T &dist ) method
definition in SpectralProfofile.cc let's say the compiler says
core/profile.cc:199: error: `kNumBins' was not declared in this scope
|
by: Olumide |
last post by:
Hi -
I've got a class that contains static member functions alone, all of
whose arguments are passed by reference as shown below:
class MySpiffyClass{
// no constructor, destructor or variables, just static members
static void FirstFunction( args & );
static void SecondFunction( args & );
static void ThirdFunction( args & );
|
by: Juha Nieminen |
last post by:
As we know, the keyword "inline" is a bit misleading because its
meaning has changed in practice. In most modern compilers it has
completely lost its meaning of "a hint for the compiler to inline the
function if possible" (because if the compiler has the function
definition available at an instantiation point, it will estimate whether
to inline it or not, and do so if it estimates it would be beneficial,
completely regardless of whether...
|
by: aaragon |
last post by:
Hi everyone,
I have a linking error when using gcc4.2 and static member variables.
The class template definition is something around the following:
template<>
class Element<L2_t: public Element_common<L2, Side<2,2 {
public:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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: 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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |