Solved

Visual C++ static member initialization question

Posted on 2004-08-03
9
246 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
Industry Leaders: 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

Industry Leaders: 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!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

724 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