Results 1 to 3 of 3

Thread: structures and classes

  1. #1
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default structures and classes

    I was wanting to know if it is considered bad practice to declare a structure in my class? a snippet of the class definition below,

    Qt Code:
    1. #ifndef READHEADER_H
    2. #define READHEADER_H
    3.  
    4. #include <QObject>
    5. ...
    6.  
    7. struct headerInfo{
    8. int numberOfSamples;
    9. int numberOfLines;
    10. int numberOfBands;
    11. int dataType;
    12. QString fileName;
    13. QVector<double> wavelengths;
    14. };
    15.  
    16. class readHeader : public QObject
    17. {
    18. Q_OBJECT
    19. public:
    20. explicit readHeader(QObject *parent = 0);
    21. headerInfo getHeaderInfo();
    22. ...
    23. private:
    24. ...
    25. signals:
    26. ...
    27. public slots:
    28.  
    29. private:
    30. ...
    31. headerInfo p_hdrinfo;
    32. };
    33.  
    34. #endif // READHEADER_H
    To copy to clipboard, switch view to plain text mode 

    So in my main program when include my readheader.h I can of course access the structure directly, but instead I use the getheaderinfo function which returns the structure(i don't really know what my reasoning behind that was). This code/class will never be distributed to anyone and any code that does use it will be writing. I am aware that the way I have done it also allows the structure contents to be changed anywhere in my code. Doesn't feel right.
    I should also say that the header file contains various information in various formats e.g. int, double, string and some vectors.
    I cant help but feel that this is not the right thing to do and is most likely considered really bad practice. I am still very new to C++ and Qt. I did initially have a ton of getter public functions in my class but wanted to return everything at once rather than making umpteen million calls.

    Any help/advice on such things would be really appreciated.

    Cheers
    Oz

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: structures and classes

    If the structure is tightly related to the class, it can be declared as in your code however it might be better to declare it inside the class.

    Qt Code:
    1. class readHeader : ... {
    2. public:
    3. struct headerInfo {
    4. int numberOfSamples;
    5. int numberOfLines;
    6. // ...
    7. };
    8.  
    9. headerInfo getHeaderInfo() const { ... }
    10. };
    To copy to clipboard, switch view to plain text mode 

    Note that the fully qualified name of the struct then becomes readHeader::headerInfo.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    OzQTNoob (15th February 2012)

  4. #3
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: structures and classes

    I don't think i'm the one who can give an advice about "best practices"...
    Anyway , what do you mean "I can of course access the structure directly" ? Maybe i'm missing something but it seems like you're confusing type's declaration with a type's instance. You've got a structure's declaration placed in a header file : that's it , this way you can do this :
    Qt Code:
    1. // in wherver header/source file the readheader.h is included //
    2.  
    3. headerInfo newHeaderInfoObject ;
    4.  
    5. newHeaderInfoObject.numberOfSamples = 10 ;
    6. // ...
    7. // etc.
    To copy to clipboard, switch view to plain text mode 

    However , If you had the headerInfo declaration placed within a readHeader class :

    Qt Code:
    1. // in wherver header/source file the readheader.h is included //
    2.  
    3. readHeader::headerInfo newHeaderInfoObject ; // if headerInfo declaration is placed in a PRIVATE section : ERROR
    4.  
    5. readHeader::headerInfo newHeaderInfoObject ; // if headerInfo declaration is placed in a PROTECTED section : ERROR
    6.  
    7. readHeader::headerInfo newHeaderInfoObject ; // if headerInfo declaration is placed in a PUBLIC section : OK
    To copy to clipboard, switch view to plain text mode 

    But , getting back to "I can of course access the structure directly" : since you've placed "headerInfo p_hdrinfo" in the PRIVATE section , there is no way you can access it directly .Isn't it the reason you've created methods for accessing it ?^^ So , depending on how you're gonna use the readHeader class in your application's data architecture , you'll probably have to place it in either PRIVATE or PROTECTED sections...

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

    OzQTNoob (15th February 2012)

Similar Threads

  1. structures in QT
    By rk0747 in forum General Programming
    Replies: 8
    Last Post: 5th April 2011, 23:04
  2. Data structures with qt4
    By NewLegend in forum Qt Programming
    Replies: 5
    Last Post: 25th July 2010, 21:57
  3. How to properly delete structures?
    By aarelovich in forum General Programming
    Replies: 5
    Last Post: 24th February 2009, 00:39
  4. APIs and Data structures
    By crazymoonboy in forum Qt Programming
    Replies: 0
    Last Post: 30th September 2008, 07:56
  5. Replies: 2
    Last Post: 17th April 2006, 21:30

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.