Solved

Visual C++ static member initialization question

Posted on 2004-08-03
9
244 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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