CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2018
    Posts
    165

    Constructor wrong

    I'd. like to make introductory comment saying the following code is a solution of old academic exercise so It's old code:

    Code:
    #include <iostream>
    #include "widget.h" 
    
    using namespace std;
    
    class MyButton :  public Button {
       TextField* tf;
    public:
       MyButton(TextField* t) : tf(t) {}; // costruttore
       void action(void *data);
    };
    
    void MyButton::action(void *data) {
       cout << tf->getTex() << endl;
    }
    
    int main() {
       Window* w = new Window;
       TextField* t = new TextField;
       Mybutton* b = new Mybutton;
       
       w->setTitle("Window");
       w->add(t);
       w->add(b);
    };

    I think that
    HTML Code:
    Mybutton* b = new Mybutton
    is wrong
    because when It is instantiated MyButton class, It's called Constructor of MyButton which needs TextField pointer as parameter.

    I should write this code:
    Code:
    Mybutton* b = new Mybutton(t);
    What do you think ?

  2. #2
    Join Date
    Nov 2018
    Posts
    153

    Re: Constructor wrong

    You're right.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured