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

14th Sep 2020, 10:53 AM
macced
4 Réponses
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
15th Sep 2020, 12:58 AM
Josh Greig
Josh Greig - avatar
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() {}; ...).
15th Sep 2020, 7:12 AM
macced
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.
15th Sep 2020, 8:12 AM
Josh Greig
Josh Greig - avatar
0
Thanks, I found solution.
17th Sep 2020, 4:50 PM
macced