Can c++ cast vector<Derived*> to vector<Base*>? [duplicate] - c++

This question already has answers here:
C++: How do I pass a container of derived classes to a function expecting a container of their base classes?
(4 answers)
C++: Can I cast a vector <derived_class> to a vector <base_class> during a function call?
(5 answers)
Closed 7 years ago.
I want to create a generic function which gets vector types as argument.
Derived1, Derived2, ... are derived (simply) from Base
I tried something like this:
int function (vector& foo)
It didn't work.
Could you tell me, what is the best generic solution for this problem?

Related

I can't understand this type of function pointer [duplicate]

This question already has answers here:
Obtaining a function pointer to a non static member function
(2 answers)
Function pointer to member function
(8 answers)
How do pointers to member functions work?
(2 answers)
Closed 7 months ago.
I'm starting to study Unreal Engine and I saw this code:
UCLASS()
class AMyPlayerController : public APlayerController
{
GENERATED_BODY()
void SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAction("Fire", IE_Pressed, this, &AMyPlayerController::HandleFireInputEvent);
InputComponent->BindAxis("Horizontal", this, &AMyPlayerController::HandleHorizontalAxisInputEvent);
InputComponent->BindAxis("Vertical", this, &AMyPlayerController::HandleVerticalAxisInputEvent);
}
void HandleFireInputEvent();
void HandleHorizontalAxisInputEvent(float Value);
void HandleVerticalAxisInputEvent(float Value);
};
I've understood everything but this line:
InputComponent->BindAction("Fire", IE_Pressed, this, &AMyPlayerController::HandleFireInputEvent);
What is the fourth parameter? I'm new to C++ but I've studied the basics of C while in college. What I can't understand is the "::". I know that the "&" means that the parameter is a pointer to the memory address of MyPlayerController, but what about the double points?
I assume that I'm passing as fourth parameter the HandleFireInputEvent function of the MyPlayerController class but I would like to know more about you.
Many thanks.
The fourth argument is the address of a class member function. And that function has a name within the class scope, just as when you have to implement a class member.

What does this C++ constructor in the initialization list mean? [duplicate]

This question already has answers here:
What are the rules for calling the base class constructor?
(10 answers)
How do I call the base class constructor?
(6 answers)
Closed 11 months ago.
I found this C++ constructor that uses an initialization list I don't understand what this convention is. I do know this initialization list can be use to instantiate a member of a instance with a param, but I've never seen a class constructor called from a list.
// G inherits from H
class GBackend : public HBackend {...}
// Constructor for G
// What is this HBackend constructor doing?
GBackend::GBackend(const BackendOptions& options) : HBackend(options), g_options(options) {...}

Argument passing of template class as an interface [duplicate]

This question already has answers here:
Templates polymorphism
(7 answers)
Closed 1 year ago.
Here is my scenario:
class Container {};
class Papa {};
class Child: public Papa {};
void myFunction(Container<Papa> container);
Now I want to pass two different parameters to myFunction().
int main()
{
Container<Papa> papaContainer;
Container<Child> childContainer;
myFunction(papaContainer);
myFunction(childContainer); // this line is not working
}
apparently I cannot do this.
no suitable user-defined conversion from "Container<Child>" to "Container<Papa>"
What would be the solution for this case? How Container<Papa> can be used as interface here?
This seems similar to this question here: No Suitable User Defined Conversion
although that one is using numeric types. Is there anything in myFunction, or that myFunction returns that would need to differentiate Child from Papa?

What is this C++ OOP concept for pointers called? [duplicate]

This question already has answers here:
Polymorphism in C++
(7 answers)
Closed 2 years ago.
int main() {
doSomething(something);
}
string doSomething(Thing *x);
Here, doSomething is a function and Thing is a class. Now, I also have another inherited class called subThing, and I also want to doSomething to a pointer of subThing.
What do you call the concept of using pointers to inherited classes? I am asking this so that I can research more on this topic.
Look up “Polymorphism”.
When subThing is derived from Thing, an instance of subThing is also an instance of Thing, so a subThing* pointer can be used anywhere a Thing* pointer can be used. Same thing with subThing& and Thing& references. Just watch out for “Object Slicing”. Polymorphic access to an object only works when accessing the object via a pointer or reference.

It is possible to check for a user defined constructor in C++? [duplicate]

This question already has answers here:
Detect if a default constructor exists at compile time [duplicate]
(2 answers)
Is there a way to test whether a C++ class has a default constructor (other than compiler-provided type traits)?
(8 answers)
Closed 3 years ago.
It is possible to check for a user defined constructor?
For example I have the following code
class A {};
class B {
public:
B(){/* do stuff */}
};
And I want to be able to do something like this:
printf("%d", check_for_user_default_constructor<B>::value);
printf("%d", check_for_user_default_constructor<A>::value);
and to have a true value only for class B.