473,239 Members | 1,584 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,239 software developers and data experts.

Why need static member variables?

In our project, there is a strange problem. Please see the following
codes.

////////////////////////////////////////////////////////////////////////
/// SSControl.h

#pragma pack(1)
typedef struct {
...
} Origin;

typedef struct {
...
} OPER_RESULT;

typedef struct {
...
} OPER_CTRL;

#pragma pack()

class CControl
{
public:
CControl();
~CControl(){};

// member function declarations
......

private:
char appMsgBuf[10240];
char szMTreeBuf[4096];
char ctlRef[256];
char ctlRefFC[256];
// some other member variables
CMTree tree;
......
};
////////////////////////////////////////////////////////////////////////////////
/// SSControl.cpp

CControl::CControl(): tree((unsigned char *)szMTreeBuf,
SSCONTROL_BUF_SIZE)
{
}

// some other member function implementations

As the codes, the CMTree object tree cannot be used as if it was not
initialized to
assign the buffer szMTreeBuf. Then I declare char appMsgBuf[10240],
char szMTreeBuf[4096], char ctlRef[256], char ctlRefFC[256] to be
static, it does work!

And in another CServices class delaration and implementation, it is the
same as
class CControl except #pragam pack (1) struct definitions.

Why does class CControl have to declare static buffers?

Jul 4 '06 #1
3 2334
CServices class delaration and implementation is exactly the same as
class CControl except #pragam pack (1) struct definitions.
Class CServices has 6 char array member variables, and more than
128kb in total.

Both class CControl and CServices have only one global object.
Class CServices work correctly, and class CControl must be declared
with static char array member variables.

I cannot understand it, please give me an answer. Thank you!

Jul 4 '06 #2
* Allen:
In our project, there is a strange problem. Please see the following
codes.
The code you've shown does not illustrate the problem you describe
(although it hints in a very vague sort of way, I frame no hypotheses).

Please see the FAQ on how to post.

However, some other problems are illustrated:

////////////////////////////////////////////////////////////////////////
/// SSControl.h

#pragma pack(1)
typedef struct {
...
} Origin;
#pragma is compiler-specific.

typedef for a struct is a C'ism, best avoided; in C++ write just

struct Origin { ... };

typedef struct {
...
} OPER_RESULT;
All uppercase should (preferentially) only be used for macro names.

typedef struct {
...
} OPER_CTRL;

#pragma pack()

class CControl
{
public:
CControl();
~CControl(){};

// member function declarations
......

private:
char appMsgBuf[10240];
char szMTreeBuf[4096];
char ctlRef[256];
char ctlRefFC[256];
These are both unsafe and memory hoggers: use std::string.

Hungarian notation is just evil, get rid of it.

// some other member variables
CMTree tree;
......
};
////////////////////////////////////////////////////////////////////////////////
/// SSControl.cpp

CControl::CControl(): tree((unsigned char *)szMTreeBuf,
SSCONTROL_BUF_SIZE)
{
}
szMTreeBuf is uninitialized at this time.

It isn't declared as size SSCONTROL_BUF_SIZE, and may have a different size.

The C style cast is dangerous and should be removed.

--
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?
Jul 4 '06 #3
Thank you for your reply.

Our project is involved HW and SW programmers. The HW programmers uses
VxWorks environment, while SW programmers code in VC, C, C++ and Java.
So the problem you mentioned is really existed. CControl class is
written by
HW programmer.

To the point, you said szMTreeBuf is uninitialized at this time.
CControl::CControl(): tree((unsigned char *)szMTreeBuf,
SSCONTROL_BUF_SIZE)
{
}
Why is ok in my CServices class? It is exactly the same.

Jul 4 '06 #4

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

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
1
by: Torsten Mueller | last post by:
I have to create a shared library containing C++ classes with static member variables on HPUX with aCC. I can compile and link the library and a test application but when I start the program I get...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
2
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
25
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...
3
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...
1
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. ...
6
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...
17
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...
4
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.