c++ how to call outer class's function from a functor? - functor

I have a functor defined within a class.
I want to access member function of the outer class directly from the functor.
How do I do that? I want to pass this functor to a template class elsewhere in the code. I dont want to use function pointers.
Does the functor need to keep a reference to the internal class as a member, assigned to it at the time of initilization, to be able to call that class's function?
Class MyClass //outer class
{
void DoSomething() //member function I want to access from functor
{
}
class MyFunctor //the functor who wants to access outerclass's function
{
void operator() ()
{
DoSomething() //This is a member function of outer class
}
}
};
}
say if I want to keep a local reference to the outer class inside the functor then how do I create an instance (not a pointer) to the functor and pass the pointer to the outerclass in functor's constructor?

Related

Why there is still a inline specifier when member fuctions define inside class?

The C++ ISO standard says: A function defined within a class definition is an inline function.
But look at the code as follows: leveldb-skiplist
class template <typename Key, class Comparator>
class SkipList{
public:
/*
...
*/
private:
inline int GetMaxHeight() const {
return max_height_.load(std::memory_order_relaxed);
}
};
there is still an explicit inline specifier when GetMaxHeight is defined inside class.
So, I want to know why we still need an explicit inline when a function defined within a class?
You don't need it. It's redundant. Just like repeating virtual in the declaration of an override.
The grammar allows it, because it's a function definition, and there's no additional wording that bans it.

Laravel what does a class name stands for after function parameterlist

I saw this in a code, but I can't figure it out, and I didn't find it in documentation (Most probably I don't know how to search)
so it's like that in a model:
public function functionName(int $parameter): ClassName{...}
What does the Class after the parameter list stands for?
public function functionName(int $parameter): ClassName{...}
In this case ClassName is written to specify the return type of function.
functionName returns object instance of ClassName
you can read more about return types here: https://wiki.php.net/rfc/return_types
this is the return type declarations. PHP7 introduces this syntex.
It indicates the types of value that the function will return.
you can use int float and it's not limited to array you can use your own class
public function functionName(int $parameter): ClassName{...}
it's not just readability, if the function returns anything elase then it will throw an error

how to construct or initialize an activerecord object in yii2 with arguments?

Let's say I want to construct a "player" activerecord object with using some parameters. How can I pass the parameters? Some articles suggest not overriding the __construct method, rather to use the init() function which is supposed to be called internally at the end of the construct method. But how can I pass the parameters to initialize the object?
If I pass an argument to the constructor it complaints me about:
Declaration of app\models\Player::init(app\models\Game $game) should
be compatible with yii\db\BaseActiveRecord::init()
The code looks like this:
Controller:
$game = Game::findOne($id);
$player = new Player($game);
Model:
public function init(Game $game) {
$this->game_id = $game->id;
}
Every object which extends BaseObject you can initialize your object with the $config parameter in the __construct() method.
For example: you can construct your Player or Game object by passing an array with object property parameters, like $game = new Game(['player_id' => 1]).

Laravel — How to get string value from object via model method?

I have model House with relation hasMany HousePhotos.
I try get link to main photo from table house_photos.
class House extends Model
{
public function photos(){
return $this->hasMany('app\HousePhoto');
}
public function get_main_photo(){
return $this->photos()->where('main', true);
}
}
Controller:
$house=House::find(1);
In View i use
{{$house->main_photo()->link}}
and got error.
When i use
{{$house->main_photo()}}
i got object. How to get string value of link to photo?
First of all you need to understand the difference between the Builder object and the Collection object.
As it is now, function get_main_photo returns a Builder object (actually a Relation object, which contains a Builder).
On these objects you can call function get() to finish the query and get the results. In this case you will get a Collection object, which you will be able to iterate.
Alternatively, in this case you seem to only have one 'main photo' per house, so instead you can call first():
public function get_main_photo(){
return $this->photos()->where('main', true)->first();
}
This will return the single associated model, on which you will be able to access ->link (if the model was found).
Remember at any point while debugging you can call the convenient dd function (which dumps the object passed as parameter and terminates the applicacion) to see what type of object you are dealing with, and all its attributes.

mocking a method with parameter as LIST type

I want to mock the below mentioned method.
public class MockClass {
public boolean ToBeMocked(Cinput, Coutput, List<CIOChain<Cinput, Coutput>>)
}
What should be in place of ?? in below mentioned code ?
Easymock.expect(MockClassObject.ToBeMocked(Cinput.class, Coutput.class, ??)).andReturn(true);
At the Class level, all List interfaces are the same regardless of the generic type, due to type erasure; they are only different at compile time.
So it's just List.class in place of ??.
That is,
Easymock.expect(MockClassObject.ToBeMocked(Cinput.class, Coutput.class, List.class)).
andReturn(true);
In the scope of mocking, you should really be specifying the objects that you expect to be passed to that method, like:
Easymock.expect(MockClassObject.ToBeMocked(cInputObj, cOutputObj, listObj)).
andReturn(true);
If for some reason you can't do that, you can use isA/anyObject variants:
Easymock.expect(MockClassObject.ToBeMocked(isA(Cinput.class), isA(Coutput.class), isA(List.class))).
andReturn(true);

Resources