0
How to call the function?
I have a pointer to obj BaseClass. I know the structure BaseClass (function name, arg, output type). How can, not rewrite function BaseClass in MyClass, a just declare its, and later execute its? https://code.sololearn.com/cVnOLsK4O8qG/?ref=app
4 ответов
0
It sounds like you want MyClass to inherit from BaseClass.
This would do the trick:
class MyClass: public BaseClass {
};
Now, MyClass will inherit the getA and getB methods.
Some more detailed documentation on this is at: https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
0
Thanks, but I have pointer to BaseClass in other process. I want create class in my lib and transfer to it pointer. After that, performing functions in your class, perform them in the base class. But at the same time, the maximum is only to declare functions (int getA(); or int getA() {}; ...).
0
I'm not sure what you want but hopefully this clears something up.
Just to be clear, the following will work. It compiles and runs with no error. It prints 2.
#include <iostream>
using namespace std;
class BaseClass {
public:
int getA() {
return 1;
}
int getB() {
return 2;
}
};
class MyClass: public BaseClass {
};
int main()
{
BaseClass obj;
MyClass *myClass = (MyClass*)&obj;
cout << myClass->getB();
}
There is no problem pointing at an object's base class.
Can you share roughly what you'd want to write in your main function after your desired changes are implemented?
Can you share your best guess at what you want implemented in c++ even if it doesn't compile? The closer you write to code the more likely I'll get what you're looking for.
0
Thanks, I found solution.