How to create a class object to call a function that can no longer be called (neither with the help of this instance or other instances of this class)?
- make a static variable and change the value when you first call it - splash58
- @ splash58 store for this an integer variable ??? !!! - hero
- @hero is the simplest option. - AivanF.
- @hero I think any other code will take more than a byte per class - splash58
- fourWhole variable? You probably meant only one variable? Do you feel sorry for one byte? - VladD
|
2 answers
For this, C ++ has a special function and a flag; this is done like this:
std::once_flag flag; //... class Class { Class() { std::call_once(flag, [this]{ SomeMethod(); }); } void SomeMethod() {...} } Thus, we will call SomeMethod() in the constructor once, but this will not prohibit calling this method in other places of the program, so it can be made private, but it will not prohibit calling the private method in other methods of the class. In order to completely eliminate the re-call of any code, you need to put all this code in the lambda, which is passed to std::call_once
std::once_flag flag; //... class Class { Class() { std::call_once(flag, [this] { // Тут будет код, который нужно вызывать лишь единожды }); } } - Can you show what it will look like in my case? - hero
- @hero, show your case first. You have too little data in question - ixSci
- It is just not clear why
[], the function should be called in the functor or what? - hero - 2Formally, no one bothers to call
SomeMethodseparately - ideone.com/hPeUbN The question is "which can no longer be called (neither with the help of this instance, nor other instances of this class) . " At best, it should be a lambda, whose scope is a constructor, but not a function from a class scope. In my opinion, the question should be reformulated more correctly :) - Harry - one@ixSci Well, I myself sometimes ask very theoretical questions ... :) And I was interested in this question from this point of view. On good thinking, he came to the conclusion that this is unrealistic for a member function . But I would like to make sure that the gurus say the same thing :) - Harry
|
Just start a static flag variable and process it:
foo::foo() { static bool once = true; if(once) { once = false; bar(); } } The second option is more elegant, but works only if bar returns any value, that is, not void :
int bar(); foo::foo() { static const auto once = bar(); } - I wonder if the debate about singleton will start now? - avp
- @avp, and there is nothing more to debate about, singltons in C ++ now have one form and are written elementarily. - ixSci
- @ixSci, and how can they now canonically write? - avp
- @avp,
static Object& instance(){static Object instance; return instance;}static Object& instance(){static Object instance; return instance;}. To taste, you can return the pointer - ixSci - @ixSci, then I do not understand something. The question of the vehicle is actually about singleton. Why, among the answers there is no your option, but there are 2 others, and both are strongly tabbed? - avp
|