12

I have a C++ class (class1) with a static object of another class (class2) as a private member.

I know upon using the program I will have to initialize the static object, I can use a default constructor for this (undesired value).

Is it possible to initialize the static object to my desired value only once, and only if I create an object of the containing class (class1)?

Any help would be appreciated.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
bryan sammon
  • 7,161
  • 15
  • 38
  • 48

1 Answers1

22

Yes.

// interface

class A {

    static B b;
};

// implementation

B A::b(arguments, to, constructor); // or B A::b = something;

However, it will be initialised even if you don't create an instance of the A class. You can't do it any other way unless you use a pointer and initialise it once in the constructor, but that's probably a bad design.

IF you really want to though, here's how:

// interface

class A {
    A() { 
        if (!Bptr)
            Bptr = new B(arguments, to, constructor);

        // ... normal code
    }

    B* Bptr;
};

// implementation

B* A::Bptr = nullptr;

However, like I said, that's most likely a bad design, and it has multithreading issues.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • ok I see what you mean. Yea, the pointer thing wont work cause I can gonna be making many objects of class A – bryan sammon May 08 '12 at 13:32
  • @bryansammon it doesn't matter how many you make, I will write an example. – Seth Carnegie May 08 '12 at 13:33
  • Thanks alot, you think thats a bad design habit? – bryan sammon May 08 '12 at 13:36
  • 1
    @bryansammon well, it of course depends on your situation, but I think there are more situations in which it's bad design than there are in which it's good design. I can't think of a situation in which you'd want a new seperate single object to appear after you create one of those objects. You get to make the final decision though. – Seth Carnegie May 08 '12 at 13:38
  • @bryansammon actually nevermind, that way wouldn't work, my bad. – Seth Carnegie May 08 '12 at 13:41
  • Cool, I really apprecaite your help – bryan sammon May 08 '12 at 13:41
  • For some remarks on the threading issues, impact on API design, and C++11 you might find this related question interesting: http://stackoverflow.com/questions/9507973/how-to-mitigate-user-facing-api-effect-of-shared-members-in-templated-classes – HostileFork says dont trust SE May 08 '12 at 14:09
  • You don't have to do the test in the constructor; any factory function will do. And if the object being constructed is mutable, you're not introducing any additional threading issues; you'll need a lock to access it, regardless of what you do. (On the other hand, truly mutable objects with static lifetime are rarely a good idea. Although there are exceptions.) – James Kanze May 08 '12 at 14:23
  • @JamesKanze actually I think you do have to do the test in the constructor, since what he wanted is to have the `B` object constructed _only if an `A` is constructed_. I was going to provide an example of returning a reference to a `static` variable inside a function, however that would make it where the `B` would be constructed when you first called the function, even if an `A` hadn't been constructed yet. – Seth Carnegie May 08 '12 at 14:53
  • If the factory function is only called from non-static members of `A`, that should do the trick. – James Kanze May 08 '12 at 16:18
  • @JamesKanze From what I gathered, the `B` is not only for use by `A`. – Seth Carnegie May 08 '12 at 18:38
  • @SethCarnegie That's what I understood as well. Otherwise, a singleton would do the trick. – James Kanze May 09 '12 at 07:20