Results 1 to 6 of 6

Thread: C++ Scope Issue / Stray Member Variables

  1. #1
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default C++ Scope Issue / Stray Member Variables

    Hi,

    I created a simple class that should provide Indexnumbers for a Node-Editor.

    indexhandler.h:
    Qt Code:
    1. #ifndef INDEXHANDLER_H
    2. #define INDEXHANDLER_H
    3.  
    4. enum type {NODE=0 ,PIN=1 , WIRE=2};
    5.  
    6. class Indexhandler
    7. {
    8. public:
    9. Indexhandler();
    10.  
    11. int getNodeindex() const;
    12. void setNodeindex(int value);
    13.  
    14. int getPinindex() const;
    15. void setPinindex(int value);
    16.  
    17. int getWireindex() const;
    18. void setWireindex(int value);
    19.  
    20. int requestIndex(int type);
    21.  
    22. private:
    23. int nodeindex;
    24. int pinindex;
    25. int wireindex;
    26. };
    27.  
    28. #endif // INDEXHANDLER_H
    To copy to clipboard, switch view to plain text mode 

    indexhandler.cpp:
    Qt Code:
    1. #include "indexhandler.h"
    2.  
    3. Indexhandler::Indexhandler()
    4. {
    5. setPinindex(0);
    6. setNodeindex(0);
    7. setWireindex(0);
    8. }
    9.  
    10. int Indexhandler::getNodeindex() const
    11. {
    12. return nodeindex;
    13. }
    14.  
    15. void Indexhandler::setNodeindex(int value)
    16. {
    17. nodeindex = value;
    18. }
    19. int Indexhandler::getPinindex() const
    20. {
    21. return pinindex;
    22. }
    23.  
    24. void Indexhandler::setPinindex(int value)
    25. {
    26. pinindex = value;
    27. }
    28. int Indexhandler::getWireindex() const
    29. {
    30. return wireindex;
    31. }
    32.  
    33. void Indexhandler::setWireindex(int value)
    34. {
    35. wireindex = value;
    36. }
    37.  
    38. int Indexhandler::requestIndex(int type)
    39. {
    40. switch(type)
    41. {
    42. case PIN: return getPinindex();
    43. case NODE: return getNodeindex();
    44. case WIRE: return getWireindex();
    45. default: return -1;
    46. }
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

    I instantiate it in the MainWindows constructor like this:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QDebug>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10.  
    11. Indexhandler *indexhandler = new Indexhandler;
    12.  
    13. qDebug() << indexhandler->getNodeindex();
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void MainWindow::on_pushButton_clicked()
    22. {
    23. qDebug() << indexhandler->getNodeindex();
    24. }
    To copy to clipboard, switch view to plain text mode 

    The first qDebug() prints out 0 as expected,
    but the second one (after the pushButton_clicked() returns a random number. Since this number only changes if the Program is restart it looks like a Stray Pointer to me.
    What am I doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ Scope Issue / Stray Member Variables

    Quote Originally Posted by 0backbone0 View Post
    What am I doing wrong?
    Since you say it runs, it means it compiled.

    That means you have a member variable called "indexhandler". Which is not initialized anywhere as far as we can tell.
    Access to it could even crash.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: C++ Scope Issue / Stray Member Variables

    Yes, it compiles and runs.
    And no it don't crashes.

    Qt Code:
    1. Indexhandler *indexhandler = new Indexhandler;
    To copy to clipboard, switch view to plain text mode 

    Isn't this initializing indexhandler?

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ Scope Issue / Stray Member Variables

    Isn't this initializing indexhandler?
    No, it creates new object named "indexhandler".
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. this->indexhandler = new Indexhandler;
    8.  
    9. qDebug() << indexhandler->getNodeindex();
    10. }
    To copy to clipboard, switch view to plain text mode 
    or even better
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow),
    4. indexhandler(new IndexHandler)
    5. {
    6. ui->setupUi(this);
    7. qDebug() << indexhandler->getNodeindex();
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to stampede for this useful post:

    0backbone0 (23rd August 2015)

  6. #5
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: C++ Scope Issue / Stray Member Variables

    Thank You!
    Both versions are working.
    You wrote "or even better"
    Why so?
    The fist approach make indexhandler a Member of MainWindow, rigth?
    Is this also the case with the second approach?

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ Scope Issue / Stray Member Variables

    Quote Originally Posted by 0backbone0 View Post
    Both versions are working.
    You can even write the first one without "this".

    Your orginal problem was that the local variable "hid" the member (local scope is resolved before instance scope).

    Quote Originally Posted by 0backbone0 View Post
    You wrote "or even better"
    Why so?
    It is just good practise to use the initialization list for its intended purpose.
    A const member can only be initialized that way, a complex member might be not be default constructible or doing so would be a waste if it is overwritten right after.

    Quote Originally Posted by 0backbone0 View Post
    The fist approach make indexhandler a Member of MainWindow, rigth?
    Is this also the case with the second approach?
    Neither "makes" it a member, declaring it as a member makes it a member.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 27th August 2014, 15:54
  2. Replies: 3
    Last Post: 2nd November 2013, 03:10
  3. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 15:53
  4. QDevelop debuggng - viewing class member variables
    By dbrmik in forum Qt-based Software
    Replies: 0
    Last Post: 7th January 2009, 11:40
  5. Replies: 5
    Last Post: 18th December 2007, 12:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.