Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Visual C++ static member initialization question

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
Indrawati
Asked:
Indrawati
  • 3
  • 2
1 Solution
 
AxterCommented:
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
 
IndrawatiAuthor Commented:
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
 
IndrawatiAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
itsmeandnobodyelseCommented:
>> 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
 
IndrawatiAuthor Commented:
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
 
itsmeandnobodyelseCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now