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

single object

48 views
Skip to first unread message

Kunal Goswami

unread,
Jan 30, 2023, 4:04:19 PM1/30/23
to
Hii ,
How can we assure that only one object is created for a class. And when second do not happen.

Note - don't use counter .

Someone asked me this in an interview.

--
*Disclaimer: *This email and any files transmitted with it are confidential
and intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify the
system manager. This message contains confidential information and is
intended only for the individual named. If you are not the named addressee
you should not disseminate, distribute or copy this e-mail. Please notify
the sender immediately by e-mail if you have received this e-mail by
mistake and delete this e-mail from your system. If you are not the
intended recipient you are notified that disclosing, copying, distributing
or taking any action in reliance on the contents of this information is
strictly prohibited.

Scott Lurndal

unread,
Jan 30, 2023, 4:28:35 PM1/30/23
to
Kunal Goswami <20cs...@iitbbs.ac.in> writes:
>Hii ,
> How can we assure that only one object is created for a class. And when second do not happen.
>
>Note - don't use counter .

A static class member seems like a good starting point.

Is it required to be thread-safe and/or re-entrant?

Frederick Virchanza Gotham

unread,
Jan 30, 2023, 4:39:23 PM1/30/23
to
On Monday, January 30, 2023 at 9:04:19 PM UTC, 20cs wrote:
> Hii ,
> How can we assure that only one object is created for a class. And when second do not happen.
>
> Note - don't use counter .
>
> Someone asked me this in an interview.

I'd probably have a global object "std::optional<SomeClass> g_object", and I'd protect it with an "std::recursive_mutex".

Any thread can 'emplace' or 'reset' it as they like so long as they lock the mutex first.

red floyd

unread,
Jan 30, 2023, 4:52:02 PM1/30/23
to
What Scott said. This is a well known design pattern called "Singleton".

class Singleton {
static Singleton the_singleton;
public:
static Singleton& get_object { return the_singleton; }
};

Singleton Singleton::the_singleton{};


Chris M. Thomasson

unread,
Feb 6, 2023, 1:31:55 AM2/6/23
to
On 1/30/2023 1:28 PM, Scott Lurndal wrote:
I thought that C++ has thread safe singletons? I know how to program one
from atomics.

Chris M. Thomasson

unread,
Feb 6, 2023, 1:33:19 AM2/6/23
to

Chris M. Thomasson

unread,
Feb 6, 2023, 1:34:07 AM2/6/23
to

Paavo Helde

unread,
Feb 6, 2023, 2:53:23 AM2/6/23
to
C++ has thread-safe initialization and destruction of static variables.

Accessing them meanwhile in thread-safe manner is another topic,
depending on a lot of hairy details.

Armando di Matteo

unread,
Feb 21, 2023, 12:13:23 PM2/21/23
to
Kunal Goswami wrote:

> How can we assure that only one object is created for a class. And when second do not happen.
>
> Note - don't use counter .

Would a bool count as a counter?

e.g.

#include <cassert>
class MyClass {
static bool instantiated = false;
MyClass() { assert(!instantiated); instantiated = true; }
~MyClass() { instantiated = false; }
}

would that be okay?

Mut...@dastardlyhq.com

unread,
Feb 21, 2023, 12:14:47 PM2/21/23
to
Don't help the kids with their coursework.

0 new messages