Results 1 to 3 of 3

Thread: Is the QChar constructor being called to initialize these objects?

  1. #1
    Join Date
    Aug 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Is the QChar constructor being called to initialize these objects?

    Hi,

    I am currently on Chapter 3 of C++ GUI Programming with Qt 4, Second Edition. And in this chapter it looks like two char arguments are passed to a function that requires two QChar arguments. To my understanding QChar is a class, so there must be some way that the char data is passed to the QChar class. Is this data passed to the QChar class by use of it's constructor (even though it's passed as an argument)? Is this unique to Qt or does it happen often in pure C++ also?

    I looked at the Documentation of QChar and it looks like it's possible to pass char data to a QChar object's constructor by something like this:
    example.cpp
    Qt Code:
    1. QChar foo='c';
    2. //or even...
    3. QChar bar('c');
    To copy to clipboard, switch view to plain text mode 

    sortdialog.cpp
    Qt Code:
    1. void SortDialog::setColumnRange(QChar first, QChar last)
    2. {
    3.  
    4. ...
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. void MainWindow::sort()
    2. {
    3. SortDialog dialog(this);
    4. QTableWidgetSelectionRange range = spreadsheet->selectedRange();
    5. dialog.setColumnRange('A' + range.leftColumn(),
    6. 'A' + range.rightColumn());
    7.  
    8. ...
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 


    I even made a short program to test this:
    main.cpp
    Qt Code:
    1. #include <iostream>
    2.  
    3. using namespace std;
    4.  
    5.  
    6. class myClass {
    7.  
    8. public:
    9. myClass()
    10. {
    11. banana='b';
    12. }
    13.  
    14. myClass(char tmp): banana(tmp)
    15. {
    16. //banana=tmp;
    17. }
    18.  
    19. char getBanana(){
    20. return banana;
    21. }
    22.  
    23. char banana;
    24. };
    25.  
    26. void createTMPClass(myClass tmp) {
    27. cout << "tmp is " << tmp.banana << endl;
    28. }
    29.  
    30.  
    31. int main()
    32. {
    33. cout << "Hello World!" << endl;
    34. myClass foo;
    35. myClass bar='c';
    36.  
    37. cout << "foo is " << foo.banana << "\nbar is " << bar.banana << endl;
    38.  
    39. createTMPClass('d');
    40.  
    41. return 0;
    42. }
    To copy to clipboard, switch view to plain text mode 

    OUTPUT
    Qt Code:
    1. Hello World!
    2. foo is b
    3. bar is c
    4. tmp is d
    To copy to clipboard, switch view to plain text mode 

    If my understanding is correct, this is a constructor being called not a copy constructor. What is this called when passing one data type (like 'char') to a function, where the function's argument (like 'QChar') uses that data type('char') for it's constructor? Is it called casting?

    I've been Googling and can't seem to find anything like this. I can find details around constructors and assignment operators, but not specifically of passing two data types to a function that requires two classes. Like this page doesn't describe it:
    http://www.learncpp.com/cpp-tutorial...ment-operator/
    Last edited by megaterminatorz; 30th August 2015 at 00:27.

  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: Is the QChar constructor being called to initialize these objects?

    That is an implicit type conversion.

    The C++ compiler is allowed to do these if it has an argument of type A and needs type B and there is a constructor of B that takes A (and either not other arguments or all other arguments have default values).

    In your case that is true for the char -> QChar conversion because QChar has a constructor with a single char argument: http://doc.qt.io/qt-5/qchar.html#QChar-9

    Cheers,
    _

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

    megaterminatorz (30th August 2015)

  4. #3
    Join Date
    Aug 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Is the QChar constructor being called to initialize these objects?

    Thank you so much for letting me know what this called. I'm going to google more about it and so far I think I understand it.

Similar Threads

  1. Replies: 13
    Last Post: 25th August 2015, 09:16
  2. Replies: 1
    Last Post: 30th December 2013, 14:53
  3. Replies: 1
    Last Post: 27th June 2011, 21:37
  4. From QString to QChar
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2009, 03:14
  5. Replies: 1
    Last Post: 25th July 2008, 23:44

Tags for this Thread

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.