473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to initialize a static class variable

23
Hey everyone,

I am trying to declare a static class variable (not a 'primitive' variable, but from a class i wrote) inside a class

However, I have tried several ways to initialize it and they all caused compilation errors:

Expand|Select|Wrap|Line Numbers
  1. #include "MyClass.cpp"
  2.  
  3. class A
  4. {
  5. static MyClass* m = new MyClass();
  6.  
  7. public void example()
  8. {
  9. m->doSomething();
  10. }
  11. };
  12.  
The error that gives me is "request for member `doSomething' in `A::m', which is of non-class type `MyClass ()()'"

I've also tried with the same result


Expand|Select|Wrap|Line Numbers
  1. #include "MyClass.cpp"
  2.  
  3. class A
  4. {
  5. static MyClass m();
  6.  
  7. public void example()
  8. {
  9. m.doSomething();
  10. }
  11. };
  12.  
Could someone direct me to what the solution is? Thank you
May 7 '09 #1
2 5172
scruggsy
147 100+
static members need to be initialized outside of the class definition (the only exception is for constant integers).
Expand|Select|Wrap|Line Numbers
  1. // Thingy.h
  2.  
  3. class Thingy
  4. {
  5.   static float someNum;
  6.   ...
  7. };
  8.  
  9. // Thingy.cpp
  10.  
  11. float Thingy::someNum = 5.432;
Your second example code there defines a static member function returning an object of type MyClass. I'm not sure what the compiler does with your first example.
May 7 '09 #2
RRick
463 Expert 256MB
You can new an object for a static pointer value but you have to be careful of any dependencies between the classes.

For example, assume you want two static classes newed (say A & B) and you expect A to be created before B. There is no guarantee which will be created first. This is especially troublesome if the newing is done in different source files (like A.cpp and B.cpp).

Since you are dealing with static pointers to objects, it better to use the Singleton design pattern. This will allow you to get around the dependency issues. Here B can check and make sure that A is created first.
May 9 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: nielson | last post by:
I have a class which contains (1) a class variable and (2) a method (e.g., methodA) which returns a reference to the class variable. If the variable has been assigned a value by a constant...
5
by: wim delvaux | last post by:
Hi all, I want to initialize two structures with pointers to eachother static StructA VarA = { PtrToVarB }; static StructB VarB = { PtrToVarA }; A solution that WORKS in C (using gcc 3.3)...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
6
by: markww | last post by:
Hi, I put a static member variable in my class. class CMine { static int m_nCount; }; How do I initialize it to zero? I can't do that in the constructor of the class can I? Won't that...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
9
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static Date default_date; };
6
by: r.z. | last post by:
They should be initialized before any instance is created. I have no idea in which file, in which place should I put their initialization code to be sure they are initialize only once, before any...
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have 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
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,...

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.