473,657 Members | 2,439 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 1447
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
3516
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 { lengthSV=16 }; // Length of SomeVector vector<double> SomeVector;
5
4555
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 know you wouldn't use 'C' { private: static std::vector<int> PSArray; public:
2
2821
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(){ } void Add() { } void Delete(){ }
0
1114
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 writers struggling to find themselves in the changing world of structured XML authoring and content management. Check it out and leave a comment to let us know what you think. http://www.thecontentwrangler.com The Content Wrangler, Inc.
6
17435
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 keep setting it to zero everytime a new
9
27380
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
8356
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 should initialize them in advance. However, the following code, in which I first produce two classses and then try to assign a reference of the first class to a static data member of the second class doesn't work. It gives the following compiler error:
4
1974
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 an instance of the class bla. According to a previous posting of Zeppe on 4/27... Doing that, this is my code: class bla
5
11114
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 C++ apparently doesn't allow a static constructor routine, how do I initialize my static member variable prior to allowing any instances to be constructed? TIA - Bob
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8734
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 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...
0
8608
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
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, 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...
1
6172
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4164
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...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
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 we have to send another system
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.