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.
Related
This question already has answers here:
C++: Difference Between Non-Member Function and Static Member Function?
(3 answers)
Closed 1 year ago.
So using static keyword below we can call MyMethod without creating an object of the class MyClass, my question is how is it even possible as a class is not allocated memory when the code is compiled?
static int MyMethod( int * a, int * b );
int one = 1;
int two = 2;
MyClass::MyMethod( &two, &one );
It seems you have a bit of a misunderstanding. There is no memory allocated for a class's methods every time it is made. Class methods are generally separate from their classes. I guess you could call the "Memory allocated to the member functions" the space that the opcodes take up. But yes, this is separate from the objects. It is just a language requirement that a member function that isn't static must be called by an instance of the class. Static functions intentionally don't care about this, and can be called even when it is not called by an object.
This question already has answers here:
Why use functors over functions?
(7 answers)
Closed 3 years ago.
Why would you overload the () operator in a C++ class or struct in C++11 or higher? As far as I can tell, these operators are overloaded when you want to pass objects like classes or structs into a std::thread and kick off a new thread with a package of data to go along with it, through a callable type.
But other than that, why else would you overload the () operator? Couldn't you simply do the same things in the constructor for a class or struct?
Why use
struct MyCallableStruct{
void operator() () {
dosomething();
}
}
when you could do
struct MyCallableStruct{
MyCallableStruct() { dosomething(); }
}
They're totally different.
First and most importantly, When you use operator(), it can be passed as function parameters (by object).
In contrast, when implemented by constructor, it can only be passed through templates (by class)
Second, operator() can be called several times after object created,
while construtor can only be called at constructing
All in all, they're different and useful in different scenarios
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?
This question already has answers here:
About Pointers To Functions in function declarations
(4 answers)
Closed 7 years ago.
In some C++ 98 code (meaning no, using std::function is not an option), I found the following construct:
class someClass
{
public:
typedef void callback();
void setCallback(callback c)
{
mCallback = c;
}
void callCallback()
{
if (mCallback)
mCallback();
}
private:
callback *mCallback;
};
This confused me. I am used to passing callback functions as a function pointer, so I would expect setCallback to take (*callback)() as argument.
However, the above code seems to work, and compiles without any (related) warnings.
Could someone tell me what is happening here? Is my callback function implicitly passed as a function pointer? Is it a good idea to use this instead of function pointers?
The only thing I could find is that this construction results in "parameter-declaration-clause" ambiguity (C++ 98 8.3p7). Is this the only downside? Are there any benefits?
Similarly to arrays, parameters of function type declare, in fact, a pointer to that type.
This question already has answers here:
C++ cast to derived class
(4 answers)
Closed 7 years ago.
Let's imagine a situation where I have an abstract class named 'Base' with a virtual pure method named foo(), and 2 children (Inherited1 and Inherited2) that both implement this method in their own way. Now let's say that one of these children (Inherited1) needs another method called bar() that would make no sense to implement in Inherited2.
In my main, i Have
Base * randomObject = new Inherited1();
I can't access this method using
random->bar();
What should I do. Like I said, it would make no sense to implement it in inherited2, so I can't simply put another virtual method in Base, or should I?
If you had a variable
Base* randomObject = new Inherited1();
You could cast it down to the base class
Inherited1* foo = static_cast<Inherited1*>(randomObject);
foo->bar();
But you have to be sure that the pointer is indeed of that derived class otherwise static_cast isn't safe. There are a number of ways to do this, such as storing an enum in the derived classes that you can check via a virtual getter, or checking if the result of a dynamic_cast is nullptr, but that is slower.