Object Initialization List Execution Sequence ...

I have a composite object (Easy_Motor) that uses an initialization list to initialize 2 objects when the composite object constructor is executed. Here's the class definition and the constructor template:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Easy_Motor
{						
  public:
    Easy_Motor (byte byMtrA_1_pin,
                byte byMtrA_2_pin,
                byte byMtr_Encoder_DO_pin,
                byte byMtr_Encoder_CSn_pin,
                byte byMtr_Encoder_CLK_pin,
                byte bySwitch_Home_pin,
                byte bySwitch_Limit_pin,
                boolean bSwitch_HI_LO,
		int _iEncoder_Counts_per_REV
    	       );
			
    void setHomeAngle (float fHomeAngle);
    void direction (boolean);
    void toggle_dir ();
    boolean getMtrDirection ();
    void speed (int);
    int getMtrSpeed ();
    float getMtrAngle ();	
    SwitchState getSwitchState ();
    void start ();		
    void stop (boolean);
    void toggle_run (boolean);			
    boolean isRunning ();
    Location whereAmI ();
    float moveToA_degree (float fAngle);
    float moveToR_degree (float fAngle);
    float moveTo_revs (float fRevs_Target);
						
  private:
    // References to composite objects that will be initialized in the
    // constructor using an initialization list.
    Mtr_DRV8833 _MyMtr;
    Encoder_AS5045 _MyEncoder;
			
    SwitchState _MySwitchState;
			
    byte _byEncoderCLKpin,
	 _byEncoderCSNpin,
	 _byEncoderDOpin,
         _byMtrA_1pin,
	 _byMtrA_2pin,
	 _bySwitchHomepin,
	 _bySwitchLimitpin;
};

Easy_Motor::Easy_Motor (
			byte byMtrA_1_pin,
			byte byMtrA_2_pin,
			byte byMtr_Encoder_DO_pin,
			byte byMtr_Encoder_CSn_pin,
			byte byMtr_Encoder_CLK_pin,
			byte bySwitch_Home_pin,    
			byte bySwitch_Limit_pin,
			boolean bSwitch_HI_LO,
			int _iEncoder_Counts_per_REV
   		       ) : _MyMtr(byMtrA_1_pin, byMtrA_2_pin),
     		           _MyEncoder(byMtr_Encoder_CLK_pin,
                                      byMtr_Encoder_CSn_pin,
                                      byMtr_Encoder_DO_pin)
{
  ... Other Easy_Motor Object Instantiation Code ...
}


What is the order of execution used when the constructor is called? Are the items in the initialization list processed first? Or is the constructor itself run first and then the initialization list processed? In my object above, can I count on the _MyMtr() & _MyEncoder() objects already instantiated (& their constructors run) before the Easy_Motor constructor is run? Is there a standard definition of how this should be processed or is it somewhat compiler-dependent? Hoping for the former.

Appreciate any help.
Last edited on
The order for initilisation of the items in the initialisation list is the order in which you have declared them in your class. In you example above the order will be:

1. Mtr_DRV8833 constructor called
2, Encoder_AS5045 constructor called

If I were you I would add the other member variables to your initilisation list so that they are all initialised before the opening brace of the contructor.
Last edited on
Thanks. Yes, I will add the pin initializers to the list.

Ok, so all of the initialization items are completed PRIOR to the start of the constructor code, correct? And that should be independent of compiler?
Ok, so all of the initialization items are completed PRIOR to the start of the constructor code, correct? And that should be independent of compiler?


Yes
Thank you very much.
Topic archived. No new replies allowed.