0

Having a simple class:

class A {
public:
  A() {}
  void set(int value) { value_ = value; }

private:
  int value_;
};

and its global instance:

A a;
  1. Is it OK to call method set on a not yet constructed object a? That can happen when for example a.set(123) is called from a constructor of another global object in another translation unit.

  2. Will the value in the object a set by calling a.set(123) remain when the non-parametric and empty constructor of A is later called for object a?

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96

3 Answers3

5

Is it ok to call method set on a not yet constructed object a?

No. You may not call member functions for an object that has not yet begun construction.

(Since the answer is no, your second question requires no answer.)


If you may need to access this global instance from multiple translation units during dynamic initialization, you can use the Meyers singleton technique:

A& global_a()
{
    static A a;
    return a;
}

a will be initialized when global_a() is first called. Note that in a multithreaded program you may need to concern yourself with synchronization of the initialization.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
2

When you write

A a;

a is a constructed object now. In case A is a then A default constructor was already been called

Andrew
  • 24,218
  • 13
  • 61
  • 90
  • No, the question is about a global object, which is constructed at some point before `main` begins. It might not be constructed before access from the constructor of another global object. – Mike Seymour Jul 12 '12 at 14:28
  • Not when a is a global variable, because the order of initialization of global objects is undefined and might not even be reproducible between subsequent program executions. – cli_hlt Jul 12 '12 at 14:30
  • Ah, sorry, I did not notice the 'global' – Andrew Jul 12 '12 at 14:30
0

If in 1) you mean it's ok to call set in the constructor, then yes, that's fine because it isn't a virtual method. You cannot call a virtual method in the constructor.

As for 2), what you're asking isn't really clear. The constructor is only called once (although there are ways around that sort of thing, but don't do them) and that is when the object is first created. You can't call the constructor on a a second time so the question doesn't really make sense.

KayEss
  • 2,290
  • 15
  • 31
  • No, calling any non-static member function gives undefined behaviour. – Mike Seymour Jul 12 '12 at 14:27
  • From a constructor? Can you show where in the standard that comes from? – KayEss Jul 12 '12 at 14:30
  • 1
    C++11, 3.8/6: "before the lifetime of an object has started [...] The program has undefined behavior if [...] the glvalue is used to [...] call a non-static member function of the object". The constructor is that of a different global object; `a` has not yet been constructed. – Mike Seymour Jul 12 '12 at 14:35
  • I think we're talking about two different cases then. I mean from the constructor of `a`, but I guess the question is about from some other constructor. – KayEss Jul 12 '12 at 14:37