Skip to content

C++ Gurus

The forum for all discussions in C++ land.
1.3k Topics 8.5k Posts
  • QList need to know all modifications

    Unsolved 19 Sept 2024, 09:03
    0 Votes
    18 Posts
    1k Views
    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 31 Oct 2024, 13:49
    0 Votes
    8 Posts
    638 Views
    @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.
  • 0 Votes
    13 Posts
    1k Views
    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
    321 Views
    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
    402 Views
    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 7 Sept 2024, 10:50
    0 Votes
    17 Posts
    2k Views
    @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 7 Sept 2024, 12:12
    0 Votes
    11 Posts
    1k Views
    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 7 Sept 2024, 13:13
    0 Votes
    3 Posts
    398 Views
    @SimonSchroeder Thanks, that's great to know. Marking yours as solution.
  • templated parameter packs

    Unsolved 2 Sept 2024, 13:21
    0 Votes
    1 Posts
    199 Views
    No one has replied
  • How to run Unit Test and main project ?

    Solved 1 Aug 2024, 09:41
    0 Votes
    7 Posts
    557 Views
    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
    347 Views
    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
  • how to stop copy of a large file

    Unsolved 24 Jun 2024, 13:22
    0 Votes
    7 Posts
    713 Views
    FWIW, since you are using the STL streams classes, note that you have buffer size control in the STL classes. One could weakly argue that you should use the Qt file IO methods instead of STL streams, since mixing frameworks is often the cause of problems in programming.
  • 2 Votes
    17 Posts
    2k Views
    @Pl45m4 I am now good on the algorithm. The one I am using (via @J-Hilk/ChatGPT) gives me a satisfactory answer I can work with and place my objects.
  • Some C++ questions about using <random>

    Unsolved 23 Jun 2024, 09:22
    0 Votes
    8 Posts
    1k Views
    @SimonSchroeder Sorry, but I don't think any part of my questions relate to random numbers or generators or distribution/uniformity performance. They are questions about C++ (especially the as-yet-unanswered question #3). With regard to the static-ness/number of times to call the initial "seeding" when comes with std::random_device rd; std::mt19937 gen(rd()); I only wish to reseed once and then accept the random number which comes from that, as is standard practice with random number generators (e.g. for old-style rand() we used to call srand() just once).
  • How to add "new line " to QString ?

    Unsolved 18 Apr 2024, 18:03
    0 Votes
    5 Posts
    768 Views
    QString myString = "First line\nSecond line"
  • Is it possible to prevent `delete` on a `const *`?

    Solved 8 Jun 2024, 09:09
    0 Votes
    17 Posts
    2k Views
    @SimonSchroeder said: But I'm not sure if this distinction is possible It's not. The delete operator can't be cv qualified. Here's a fun quirk: struct Foo { void itIsFine() const { delete this; } ~Foo() { bar = 42; } int bar = 0; }; const Foo* foo = new Foo(); foo->itIsFine(); so not only can you delete an object through a pointer to const, but a const function can mutate the object without mutable or const_cast by deleting the object it is being called on;)
  • __attribute__((???)) ignored

    Solved 4 Jun 2024, 17:59
    0 Votes
    5 Posts
    579 Views
    @jeremy_k that is weird. some examples place the directive after the declaration and others before like it shouldn't matter...but you are right. It did make a different. Placing it before the declation cause the warning to be igrnored. looks like gcc documention is wrong...go figure. LOL wrong in TWO PLACES...First, they say "unused" is a valid attribute, and second, internal examples sometimes place the attribute after the decl. Hmmm.
  • can't compare lists of my struct

    Solved 31 May 2024, 02:58
    0 Votes
    8 Posts
    717 Views
    @mzimmers said in can't compare lists of my struct: I removed the QObject derivation, and now it seems to work. If you don't plan to ever use meta-object related stuff, then you really don't need a QObject derived class or the Q_OBJECT macro (for metacalls / qt signals).
  • Same question

    Unsolved 29 May 2024, 01:53
    0 Votes
    2 Posts
    279 Views
    Which Qt Version and C++ standard are you using?
  • 0 Votes
    14 Posts
    3k Views
    I have a same question,but I can't change Qt Version or c++ standard. do you have other suggestions? Thank you in advance for your help