473,505 Members | 13,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialize static structured variable challenge

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) is

static StructA VarA; // defining VarA
static StructB VarB; // defining VarB

and then

static StructA VarA = { &VarB };
static StructB VarB = { &VarA };

However in C++ (same gcc version) it does not work because
the first static is not assumed to be tentative.

How can I solve this problem ? Initializing using
functions is not possible since the above code is
generated automatically by some compiler

W
Jul 22 '05 #1
5 1437
wim delvaux wrote:
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) is

static StructA VarA; // defining VarA
static StructB VarB; // defining VarB

and then

static StructA VarA = { &VarB };
static StructB VarB = { &VarA };

However in C++ (same gcc version) it does not work because
the first static is not assumed to be tentative.

How can I solve this problem ? Initializing using
functions is not possible since the above code is
generated automatically by some compiler


namespace
{
extern StructA VarA;
extern StructB VarB;

StructA VarA = { &VarB };
StructB VarB = { &VarA };
};

I didn't test it, but the above should compile. Note that the "extern"
in an anonymous namespace has about the same meaning as static.
Jul 22 '05 #2
namespace {
typedef struct x_s {
int x;
} x_t;

extern x_t X1;
extern x_t X2;

static x_t X1 = { &X2 };
static x_t X2 = { &X1 };
}

Compiled above code ...

u19809@buro:.../tmp$ c++ --version
c++ (GCC) 3.3.3 (Debian 20040417)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Result :

u19809@buro:.../tmp$ c++ t.c -o t++
t.c:9: error: `<unnamed>::X1' was declared `extern' and later `static'
t.c:6: error: previous declaration of `<unnamed>::X1'
t.c:9: error: invalid conversion from `<unnamed>::x_t*' to `int'
t.c:10: error: `<unnamed>::X2' was declared `extern' and later `static'
t.c:7: error: previous declaration of `<unnamed>::X2'
t.c:10: error: invalid conversion from `<unnamed>::x_t*' to `int'

Jul 22 '05 #3
wim delvaux wrote:

What you tried is totally different from what Gianni suggested.
The 'static' keyword has managed to sneak back in. I compiled
the following, as a complete translation unit. There were no
errors on GCC 3.3.1, MSVC 7.1 or BCB 6.0.

namespace
{
struct StructB;
struct StructA { StructB * p; };
struct StructB { StructA * p; };

extern StructA VarA;
extern StructB VarB;

StructA VarA = { &VarB };
StructB VarB = { &VarA };
};

void f()
{
StructA a;
StructB b;
}

--
Regards,
Buster.
Jul 22 '05 #4
Ah ok,

one thing I am afraid of is that VarA and VarB should remain local
variables (i.e. not cause nameclashes in other modules).

Does the use of the anonymous namespace imply that the vars are hidden for
all other source files ?

W
Jul 22 '05 #5
wim delvaux wrote:
Ah ok,

one thing I am afraid of is that VarA and VarB should remain local
variables (i.e. not cause nameclashes in other modules).

Does the use of the anonymous namespace imply that the vars are hidden for
all other source files ?


yes - (modulo compiler bugs).
Jul 22 '05 #6

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

Similar topics

10
3502
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
5
4546
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...
2
2808
by: Vinu | last post by:
Hi, I am facing one problem related to global variables in .so file. When ever I access the variable the application is crashing I have a class called services. class services { services(){...
0
1104
by: Scott Abel | last post by:
Tony Self of HyperWrite presents an interesting and informative article entitled "Semantic, Structured Authoring: The Challenge for Technical Writers" that is sure to be of use to many technical...
6
17423
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...
9
27370
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; };
4
8321
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...
4
1965
by: Bram Kuijper | last post by:
Okay, second try (since my posting on 4/27), to find a proper way to initialize a static reference member to an object. I try to initialize a static reference inside the class ga, referencing to...
5
11103
by: Bob Altman | last post by:
Hi all, I have a private static structure in a C++ class (it's a CRITICAL_SECTION structure) that needs to be initialized by passing its address to a routine (InitializeCriticalSection). Since...
0
7213
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
7366
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...
0
7471
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
5610
agi2029
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,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
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 ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
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...

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.