Solved

Visual C++ static member initialization question

Posted on 2004-08-03
9
238 Views
Last Modified: 2013-12-14
Hi

Let's say I have a class A. A has a static member whose type is another class, let's call it class B. Usually in my C++ application I declare this static member at the top of A's .cpp file as follows:

B A::instanceofB(aString)

where aString is of std::string type and is the argument for B's constructor.

That works OK and I got the result I wanted. However, yesterday I use that file on a different project (it's a directshow filter project) and it seems that instanceofB is never initialized there! I tried to debug it by making B's constructor to write to a file, but when I checked, the file was never written into, it's as if B's constructor's never called at all. I did that for B's default constructor too, but with the same result. I checked in all the project settings but I couldn't find out what causes this discrepancy.

Could someone point me on what's the possible causes for this?

Thanks!
0
Comment
Question by:Indrawati
  • 3
  • 2
9 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Are you calling the static member before main() is called?
In other words, is another static or global object calling your static object?

If you have two static/global objects calling each other, and they're in different source files, there's no garantee that they'll be initialize in proper oder.
0
 
LVL 3

Author Comment

by:Indrawati
Comment Utility
Hi

Thanks for the reply. The static variable is a private member variable, and it's accessed only via one static member function. This function is never called before the program has been run.
0
 
LVL 3

Author Comment

by:Indrawati
Comment Utility
OH, and BTW, no other static member variables access the static variable.

Currently my trick to work around this issue is by defining an assignment operator in Class B and adding a few lines in A's static member function that accesses the static member function as follows:

if(instanceofB not initialized)
    instanceofB = B();
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>> B A::instanceofB(aString)

where 'aString' is defined?

The problem of static class instances having class arguments like std::string object is, that the arguments must be instantiated earlier. In your example you should have

    std::string aString = "Any String";
 
    B A::instanceofB(aString);

However, better is

    B A::instanceofB("Any String");

what compiles if the constructor of B is declared as

   B(const std::string& aString);


If you defined aString in another cpp file or aString is another static member variable, that might go wrong as the order of static instantiation could be arbitrary.

Regards, Alex
0
 
LVL 3

Author Comment

by:Indrawati
Comment Utility
Thanks for the reply. aString is defined in an included header file, and it's declared as follows:

const std::string aString = "Anything goes here";
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 70 total points
Comment Utility
The problem with defining variables in a header file is that you can include that header only from one cpp file. If included by a second cpp file the linker would complain because of a duplicate instance.

Then, because 'aString' isn't a static class member it got instatianted later than A::instanceOfB what may cause your problem.

I would suggest, either to use the initialisation using a const char* instead of a const std::string as i posted above OR define aString as a static member of class A  ABOVE 'instanceOfB'.

Regards, Alex
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now