Solved

Visual C++ static member initialization question

Posted on 2004-08-03
9
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 11712361
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
ID: 11712406
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
ID: 11712425
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11712746
>> 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
ID: 11712886
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
ID: 11713170
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

737 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