I've a Class called Layout which is the parent class of MyLayout
e.g. MyLayout extends Layout.
Now another Class App takes Layout& argument on one of its constructor.
App Class have a member variable called layout on which an instance of Layout Class exists.
which is meant to be optionally instantiable from constructor.
App Class have an exec() method that calls layout.fnc()
Layout's fnc() is virtual
Now the problem is I should be able to do App app(MyLayout) as well as App(Layout)
as MyLayout is a Child class of Layout Class.
The Code is being Compiled if I pass a MyLayout Class in constructor's argument.
but It is calling Layout::fnc() instead of MyLayout::fnc()
-------------- All My codes are Attached ------------------
layout.h
layout.cppCode:#include <iostream> using std::cout; using std::endl; class Layout{ public: Layout(); ~Layout(); public: virtual void func(); };
mylayout.hCode:#include "layout.h" Layout::Layout(){ } Layout::~Layout(){ } void Layout::func(){ cout<<"Layout::func()"<<endl; }
mylayout.cppCode:#include "layout.h" class MyLayout : public Layout{ public: MyLayout(); ~MyLayout(); public: virtual void func(); };
app.hCode:#include "mylayout.h" MyLayout::MyLayout(): Layout(){ } MyLayout::~MyLayout(){ } void MyLayout::func(){ cout<<"MyLayout::func()"<<endl; }
app.cppCode:#include "layout.h" class App{ public: Layout layout; public: App(); App(Layout& lt); ~App(); void exec(); };
main.cppCode:App::App(){ } App::App(Layout& lt):layout(lt){ } App::~App(){ } void App::exec(){ layout.func(); }
Code:#include "app.h" #include "mylayout.h" using namespace std; int main(int argc, char *argv[]){ MyLayout l; App a(l); a.exec(); return EXIT_SUCCESS; }



LinkBack URL
About LinkBacks




CornedBee