Google Groups จะไม่รองรับโพสต์หรือการสมัครสมาชิก Usenet ใหม่อีกต่อไป โดยคุณจะยังคงดูเนื้อหาเดิมได้อยู่

single object

ยอดดู 48 ครั้ง
ข้ามไปที่ข้อความที่ยังไม่อ่านรายการแรก

Kunal Goswami

ยังไม่อ่าน,
30 ม.ค. 2566 16:04:1930/1/66
ถึง
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

ยังไม่อ่าน,
30 ม.ค. 2566 16:28:3530/1/66
ถึง
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

ยังไม่อ่าน,
30 ม.ค. 2566 16:39:2330/1/66
ถึง
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

ยังไม่อ่าน,
30 ม.ค. 2566 16:52:0230/1/66
ถึง
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

ยังไม่อ่าน,
6 ก.พ. 2566 01:31:556/2/66
ถึง
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

ยังไม่อ่าน,
6 ก.พ. 2566 01:33:196/2/66
ถึง

Chris M. Thomasson

ยังไม่อ่าน,
6 ก.พ. 2566 01:34:076/2/66
ถึง

Paavo Helde

ยังไม่อ่าน,
6 ก.พ. 2566 02:53:236/2/66
ถึง
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

ยังไม่อ่าน,
21 ก.พ. 2566 12:13:2321/2/66
ถึง
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

ยังไม่อ่าน,
21 ก.พ. 2566 12:14:4721/2/66
ถึง
Don't help the kids with their coursework.

ข้อความใหม่ 0 รายการ