Skip to content

C++ Gurus

The forum for all discussions in C++ land.
1.3k Topics 8.5k Posts
  • The Question of a Lifetime

    Unsolved
    5
    0 Votes
    5 Posts
    209 Views
    Kent-DorfmanK

    @SimonSchroeder
    Your description above is in substance, correct. I had to check it to make sure myself. ;^) By assigning the string literal to an array (const or not) it is considered "auto class". When you create a pointer to the string literal the pointer will "typically" point to a RO-data segment containing the literal.

  • 0 Votes
    1 Posts
    23 Views
    No one has replied
  • QGiS in WIndows

    Unsolved
    1
    0 Votes
    1 Posts
    40 Views
    No one has replied
  • Problem using overridden methods

    24
    0 Votes
    24 Posts
    12k Views
    Pl45m4P

    @osirisgothra said in Problem using overridden methods:

    I do really like being able to do this:

    which opens a wonderful window that

    You could write a QtCreator extension/plugin and try to find out how to get your hand on the data you need.

    displays EVERY child class of that object with a checkbox by each function. You can just check off each signature you want to override

    Probably via the Code Model / Symbols.

    If you wanna try it, start a new topic.
    People who have more experience with such things than I do may be able to help you then.

  • Unable to create C++ google test for the C++ class

    Unsolved
    6
    0 Votes
    6 Posts
    242 Views
    Christian EhrlicherC

    See https://doc.qt.io/qt-6/debug.html : set QT_FATAL_WARNING, run your app in the debugger and see where you create a QWidget before a QApplication.

  • To singleton or not to singleton, that is the question....

    Solved
    23
    0 Votes
    23 Posts
    1k Views
    S

    @JoeCFD said in To singleton or not to singleton, that is the question....:

    Calling baz() with using "using Foo;" can cause confusing while two namespaces are present at the same place.

    In the concrete example you have given, it would not compile because the compiler cannot resolve the overload (i.e. Foo1::baz or Foo2::baz). You'd still need to use a fully qualified name. It only gets confusing when you have Foo1::baz(int) and Foo2::baz(double). You might write baz(1); when you actually want to call Foo2::baz. The software might even do the right thing for years. Someone might just introduce Foo1::baz years down the line and suddenly the behavior changes.

  • Template class as parameter question

    Solved
    6
    0 Votes
    6 Posts
    234 Views
    S

    @JonB said in Template class as parameter question:

    What happens when another container has a different definition of count() which does not return an int (or number)? Maybe your syntax is not quite correct and it would have to be requires(T t) { int t.count(); }?

    I guess this would be some additional requirement using std::is_same_v and decltype(t.count()). I'm still on C++17, so I haven't used concepts myself.

    @JonB said in Template class as parameter question:

    Hmph! But that's is just what e.g. QList is, and loads of other containers.

    A container class is an obvious example where a general purpose implementation makes sense. But, you usually don't implement your own containers. Furthermore, most of the time I would have at least int and double in mind for containers. So, making them general purpose from the get go is a given. But, I don't have to make all my algorithms general purpose such that they might be reusable decades down the line if ever. If there is a current need for general purpose code, make it general purpose. Otherwise it just make maintenance more complicated while being unnecessary. I guess it might also introduce additional bugs because you try to be extra clever.

  • Subclassing where parent class has `[]` operator

    Unsolved
    11
    0 Votes
    11 Posts
    404 Views
    S

    Well, first of all (user-defined) operators in C++ are just functions. Hence, it means you can call them using the function syntax (like mentioned as operator[](someIndex)). To understand why you cannot just write [someIndex] we need to look at the history of C++; or rather C. We get the index operator syntax from plain arrays and pointers. These require the array or pointer to be explicitly written before the operator: p[i] or array[i]. (To be more precise: These are the same as i[p] and i[array] because officially in C these are equivalent to p+i=i+p and array+i=i+array. However, this weird syntax does not work for operator[] overloading and doesn't help to make my point.) In the same way you need to write the object on which to call the overloaded operator[] in front of the index access. If you could use implicit this, the same would need to apply e.g. for the binary operator+ and the like. What about operator=? I personally also think that just [someIndex] is not really readable. It doesn't show directly that you want to use an index operator. I prefer that you must write (*this)[someIndex] explicitly. In most places I would use it like this. Only when I want to make plain that I want to forward to operator[] (as was initially asked) I would make the forwarding aspect clear by writing operator[](someIndex).

    Furthermore, now it is too late to introduce the short-hand syntax (implicit this) into C++. [] without an object preceeding it is already reserved for introducing lambdas. I would expect that the compiler error for return [someIndex]; would say something along the lines that the body for the lambda function is missing. (return [someIndex]{}; would return a lambda that does nothing, but captures someIndex by value. Function arguments () are optional for lambdas if they are empty.)

    Sometimes when I need to use some operators (not necessarily the index operator) several times inside a member function (and I'm annoyed at writing (*this) over and over again) I would write auto &self = *this;. Then, it is much nicer to write return self[someIndex];. (The name self is derived from other OO languages that use a reference self to the object instead of a pointer this.)

  • SIGNAL/SLOT() macros in Qt's source code

    Solved
    19
    0 Votes
    19 Posts
    741 Views
    SGaistS

    @GrecKo said in SIGNAL/SLOT() macros in Qt's source code:

    @Pl45m4 said in SIGNAL/SLOT() macros in Qt's source code:

    The string-based connection does not need QObject receiver.

    The signature is QObject::connect(const QObject *, const char *, const QObject *, const char *, Qt::ConnectionType ) though

    This was referring to this overload:

    QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type = Qt::AutoConnection) const

    Which implies that the target is the current object.

    This is different from the similar (yet different) PMF variant as there's no implicit target in that case. Hence the recommendation to use the variant which has a context object.

  • QList need to know all modifications

    Unsolved
    18
    0 Votes
    18 Posts
    775 Views
    S

    Each class has its own vtable (it is not per object). Each object then has a pointer to the vtable. When you have a pointer to a base class pointing to an object of a derived class, it just uses the pointer to the vtable and calls the appropriate function. Like mentioned before, this is only one indirection. (It gets more complicated with multiple inheritance.)

    Concerning Objective-C: Most strings for message are compile-time known strings. This means that "string comparison" in reality is most of the time just comparison of pointers. The runtime also uses a little cache to speed up the most recent function calls.

    BTW: Since we are always talking about performance here. There is a CppCon talk on Youtube "Optimizing Away C++ Virtual Functions May be Pointless" https://youtu.be/i5MAXAxp_Tw?si=ieyiW3G31UMmANV-

  • QObject-inherited classes' virtual destructor method

    Solved
    8
    0 Votes
    8 Posts
    291 Views
    JonBJ

    @J-Hilk
    Yes, as you say, it comes from UI app. I often pick the UI template for a test project so I get the QApplication and necessary Makefile/cmake stuff but then uncheck the "create .ui file" box as I don't want that and create widgets in code. Which leaves me with the empty destructor code. I am now happy that I don't need that unless I want it.

  • Algorithm: finding the number contained within most intervals

    Unsolved
    13
    0 Votes
    13 Posts
    741 Views
    S

    Here is another idea: Treat the intervals as sets and compute their intersections.

    Start with the first interval and compute the intersection with every other interval. Store the intersections inside a second vector. The second vector should now have n-1 elements (intersection of 1st with 2nd, 1st with 3rd, 1st with 4th, ..., 1st with nth, but not 1st with 1st). Now you take the intervals in this second vector and intersect them with all original intervals again (first interval in the second vector is for 1st with 2nd, so we only need to start intersecting starting from 3rd, for the second interval we start from the 4th interval, and so on). The intersections are then stored inside a third vector. If there are only empty intervals inside the third vector, we can stop for now. Otherwise we throw away the second vector and repeat this process with the third vector. (The second vector is intersections containing 2 intervals, the third vector contains intersections of 3 intervals, so we don't need the second vector anymore if there are non-empty intervals in the third vector.) From the last vector we get from this process we keep one intersected interval (just take the first non-empty intersection) and remember it. It is the intersection with the most intervals including the first interval. Now, we repeat this for the second interval, and so on.

    This should give as for each interval the maximum of intersections containing this interval and the interval limits of the intersection. You can just pick the intersected interval with the highest number of intersections and use the minimum of that interval as the number.

    It is a little tricky to wrap your head around this (initially I thought it would be little easier). The way I described it runtime would be at least O(n³). I'm not sure if this is good or bad.

  • 0 Votes
    2 Posts
    160 Views
    J

    Never Mind, turns out there was a bug elsewhere in my code. Both methods work lol. I should really call it a day...

  • 0 Votes
    2 Posts
    178 Views
    SGaistS

    Hi,

    If you want to to mock QSerialPort you have to make it replaceable in your class.
    Use a QIODevice pointer as member and either add a setter or a constructor parameter so you can use either QSerialPort or your own mock device.
    This means that you need to refactor your logic a bit to setup the port.
    On a side note, your class has two different things that look suspicious:

    It seems to be doing way too many things seeing its internal and the class name singleton implementation more often than not are architectural issues.
  • Qt6 C++ compilation changes

    Unsolved
    17
    0 Votes
    17 Posts
    1k Views
    JoeCFDJ

    @JonB We learn more from discussions. I do not feel you are difficult in any way.

  • Qt Creator Code Model error on legal attribute

    Solved
    11
    0 Votes
    11 Posts
    574 Views
    JonBJ

    UPDATE/CORRECTION

    I tried again with switching Use clangd on and off. Somehow it seems it didn't "take" right formerly. Now I do see different behaviour. I find that sometimes when I start Creator, it warns me I have don't have enough memory for clangd, I selected "Enable anyway", it still red-underlines [[fallthrough]];. Then I go to Preferences, switch off clangd and then switch it back on, that clears the complaint. It seems I have to switch off and back on every time to make it work. Sigh....

    With clangd switched on I can use any of the [[fallthrough]]; annotations which get through gcc compiler and code editor does not red underline them. Q_FALLTHROUGH() macro still requires a blank line without clangd, but works without blank line with clangd. Fair enough, @aha_1980 has warned me that it will be "problematic* with the internal code model rather than clangd.

    So all in all I can now use [[fallthrough]]; in my code with clangd, that is apparently "standard" C++ 17 so I accept that.

  • Qt6 linking change

    Solved
    3
    0 Votes
    3 Posts
    239 Views
    JonBJ

    @SimonSchroeder Thanks, that's great to know. Marking yours as solution.

  • templated parameter packs

    Unsolved
    1
    0 Votes
    1 Posts
    127 Views
    No one has replied
  • How to run Unit Test and main project ?

    Solved
    7
    0 Votes
    7 Posts
    365 Views
    SGaistS

    Hi,

    The best way to do it is to create a subdirs project as described here. Even though it shows it for an application with libraries, the same concept applies to adding tests. You can check the Qt 5 sources, they are using that concept there.

  • 0 Votes
    2 Posts
    200 Views
    sierdzioS

    Doxygen is a good choice, I don't know if it will fit your requirements exactly though. It can show nice class-subclass hierarchies if you enable dot diagrams. In cmake this can be done like so:

    find_package(Doxygen REQUIRED dot)

    (https://www.doxygen.nl/manual/diagrams.html)

    This will result in something like this: https://www.doxygen.nl/manual/examples/diagrams/html/class_c.html