How to avoid two step initialization (C++)?

I’d like to follow the RAII(resource acquisition is initialization) idiom throughout my code but I’m also doing the template pattern where I’m developing generic versions of my classes and using them to build a common codebase for certain things. Sometimes I need to enforce an initialization sequence where I would need to call the specialized object’s virtual functions in the constructor but that’s not possible in C++. The only solution I can think of is a two step initialization by calling an init function after the object is created but that breaks the RAII idiom. Is there any solution to this?

#include <memory>  class A { public:     A() {         // I want to call B's foo() here     }     virtual void foo() = 0; };  class B : public A { public:     B() {};     virtual void foo() {}; };  void main() {     std::unique_ptr<A> a(static_cast<A*>(new B));      // Use b polymorphically from here... } 

I’m Being Bullied, How Do I Avoid Being Suspended?

I've recently gotten attention from an "exposure" account – someone who goes around looking for people he doesn't like, putting them on display, and telling his 800+ followers to report them.

I ended up on his bad side and I've likely been reported on several accounts by this guy and his rabid followers. 5 of my accounts in total have been targeted by him (because I stupidly listed them all in my Lists).

So I'm sitting here waiting for Twitter Support bots to decide whether to suspend me…

I'm Being Bullied, How Do I Avoid Being Suspended?

Encapsulating Permissions Logic, but looking to Avoid Circular Dependency

I’m trying to encapsulate permissions logic for a particular view model in a way that the permission logic has access to the view model object, but is also exposed inside of it

Trivial Implementation:

public class ClientViewModel {     public Client Client { get; set; }      /* permissions section */     public bool CanVote => Client.Age > 18     public bool CanDrink => Client.Age > 21 } 

The implementation is pretty clean and simple. The view will need to make lots of decisions based on the set of properties available within the permissions. But there are going to be a lot of permissions so ideally I’d like to contain that logic somewhere else.

Right now I can access like this:

var vm= new ClientViewModel() { Client = myClient }; vm.CanVote 

But I’d like to contain all the logic inside a single class and access like this:

vm.Permissions.CanVote

Circular Implementation

So I can put a property of type ClientViewModelPermissions on the ViewModel itself. It needs to have access to data objects on the ViewModel it’s describing so I can pass in the instance of the model into the Permissions constructor and new it up during the construction of the model itself, like this:

public class ClientViewModel {     public ClientViewModel()     {         // create instance of permissions for current object         Permissions = new ClientViewModelPermissions(this);     }      public Client Client { get; set; }      public ClientViewModelPermissions Permissions { get; set; } }  public class ClientViewModelPermissions {     public ClientViewModelPermissions(ClientViewModel clientVm)     {         // permissions must describe a particular view model         ClientModel = clientVm;     }      private ClientViewModel ClientModel { get; set; }      public bool CanVote => ClientModel.Client.Age > 18     public bool CanDrink => ClientModel.Client.Age > 21 } 

So each class contains a reference to the other as a property. Should this be avoided for any reason? Is there a cleaner way to evaluate properties for a given class, but keep that logic separate than actual class itself by decorating it somehow?

How to find efficiently the minimum modification to avoid close consecutive numbers?

I have an array of sorted numbers:

arr = [-0.1, 0.0, 0.5, 0.8, 1.2] 

I want the difference (dist below) between consecutive numbers for that array to be above or equal a given threshold. For example, if threshold is 0.25:

dist = [0.1, 0.5, 0.3, 0.4] # must be >=0.25 for all elements 

arr[0] and arr[1] are too close to each other, so one of them must be modified. In this case the desired array would be:

valid_array = [-0.25, 0.0, 0.5, 0.8, 1.2] # all elements distance >= threshold 

In order to obtain valid_array, I want to modify the minimum amount of elements in arr. So I substract 0.15 from arr[0] rather than, say, substract 0.1 from arr[0] and add 0.05 to arr[1]:

[-0.2, 0.05, 0.5, 0.8, 1.2] 

Previous array is also valid, but we have modified 2 elements rather than one. In order to obtain valid_array, I already have a brute force solution which works fine, but it is quite slow for large arrays. My questions are:

What is the time complexity of that brute force solution?

Does a more efficient algorithm even exist?

Edit

First, I need to clarify what I mean by difference, which I define the same way as in here, so out[n] = a[n+1] - a[n]. The fact that all elements in that difference must be above (or equal) threshold implies that valid_array is also sorted.

Second, the number of modifications (which must be minimized) is obtained by comparing elementwise the original arr and valid_array

How to avoid background image display in Windows 10

I could disable background image display at windows Logon by going to Settings, Personalization, Lock Screen.

But the problem is that the background image still appears when monitor is turned on after it was turned off due to inactivity. To get rid of the image, I have to use the finget to move the image toward the top of the screen (this is a touch screen).

How can I disable background image completely?

Thanks Jaime

Avoid having the 0 returned by index() displayed

I have a function returning some values from a different table:

=IFERROR(INDEX(Table2[Kommentar];[@[Radnr.]]);"") 

This works great for most cases, but if there’s no information entered into the column in the other table, I get a 0 when I would have preferred the cell to display the same value as the other table, i.e. stay blank.

Is there any good way to do this?

Current results

enter image description here

Preferred results

enter image description here

How to avoid malware DIY

Most “how tos” regarding malware list this:

  1. Install Antivirus software.
  2. Install latest antivirus software.
  3. Run antivirus software regularly.
  4. Install latest operating system.
  5. Install any patches to software.
  6. Don’t download random stuff and run it as an application (like .exe files).

I would like to know in a little more detail what all those first steps mean, and what else could be included in this list. For example, LittleSnitch blocks all outgoing and incoming traffic and lets you inspect it first. That would be a good thing to do. Then there is potentially being alerted whenever a script tries to be invoked that you didn’t write yourself, or isn’t installed from a trusted vendor. I’d like to know what other kinds of things you should do beyond this list to have an ideal security environment in regards to avoiding malware.

Collection in java which can avoid duplicates values of object

public static void main(String[] args) {

     HashSet<employee> set=new HashSet<>();           //Creating Books        employee b1=new employee(101,"Let us C","Yashwant Kanetkar","BPB",8);        employee b2=new employee(101,"Let us C","Yashwant Kanetkar","BPB",8);        employee b3=new employee(103,"Operating System","Galvin","Wiley",6);           //Adding Books to HashSet           set.add(b1);           set.add(b2);           set.add(b3);           //Traversing HashSet           for(employee b:set){           System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);           }     } 

what i need is , above code should not add duplicate value, like b1 and b2 are same, so only 1 value should be added