Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unable to push back objects to a static vector in the constructor

39 views
Skip to first unread message

aligh...@gmail.com

unread,
Mar 9, 2017, 12:06:00 AM3/9/17
to
//Below is the declaration in the .h file


static std::vector<TimerClass*> timeKeeper; //static vector to hold all time objects

//Below is the initialization in the .cpp file

std::vector<TimerClass*> TimerClass::timeKeeper;

//Below is my constructor

TimerClass::TimerClass(const Mhandler<ProductClass> &on, const Mhandler<ProductClass> &off)
:trigger(0), subTrigger(0),interval(0),subInterval(0),
features{Mode::FREE,Mode::ONE_SHOT,Mode::ALARM},
onHandler(on),offHandler(off), runOnce(false)
{
//TimerClass::timeKeeper.push_back(this);
//The above code does nothing...???
//timeKeeper.size() == 0;
}


//The below method works but the disadvantage is that i have to call it every //time i make a TimerClass object

void TimerClass::attach(TimerClass *obj) {
TimerClass::timeKeeper.push_back(obj);
}

Why is it that the code in the constructor body does not push back the &TimerClass obj created by the constructor into the static vector timekeeper?

Any help in understanding this issue will be highly appreciated

Thank you!

Paavo Helde

unread,
Mar 9, 2017, 1:18:03 AM3/9/17
to
Maybe because it is commented out?

Please extract and post the minimal "compiling" example of the problem.

Also note that this kind of registration does not work well if the
TimerClass is a base class from which other classes are derived. The
problem is that in the base class constructor the object is not yet
complete and making an incomplete object accessible to other code via a
global structure may cause a bunch of nasty surprises (especially in a
multi-threaded program). Ditto for de-registration in the destructor.

Also, using raw pointers like that is seeking for troubles. Nowadays I
would use std::weak_ptr pointers in such registries instead.

hth
Paavo

Ali Ghadyali

unread,
Mar 10, 2017, 7:26:25 PM3/10/17
to

> Maybe because it is commented out?
>

I commented it out to show the code that does not work!

> Please extract and post the minimal "compiling" example of the problem.

I will do this shortly

>
> Also note that this kind of registration does not work well if the
> TimerClass is a base class from which other classes are derived. The
> problem is that in the base class constructor the object is not yet
> complete and making an incomplete object accessible to other code via a
> global structure may cause a bunch of nasty surprises (especially in a
> multi-threaded program). Ditto for de-registration in the destructor.

It is not a base class. It is used in other classes by composition not inheritance.

>
> Also, using raw pointers like that is seeking for troubles. Nowadays I
> would use std::weak_ptr pointers in such registries instead.

will look into those. This is on an embedded platform based on an arm cortex M3

Juha Nieminen

unread,
Mar 13, 2017, 3:15:49 AM3/13/17
to
Ali Ghadyali <aligh...@gmail.com> wrote:
>> Maybe because it is commented out?
>
> I commented it out to show the code that does not work!

"Does not work" isn't very helpful. Describe what is it that you are getting.
0 new messages