Skip to content
  • 0 Votes
    3 Posts
    269 Views
    M

    @JonB hello, yes I am an beginner in programming. I have like 2 months experience with it. Would you give me an example for my problem please ?

  • 0 Votes
    3 Posts
    312 Views
    Q

    Thank you, that solved my problem :)

    Now that you wrote it, i remember how it worked in c++. Only one cpp file QVector<gUser> Users; and others extern QVector<gUser> Users; for access.

    Thanks!

  • 0 Votes
    5 Posts
    10k Views
    Linus JahnL

    Thanks for your replies.

    It probably doesn't matter at all, because the only result will be that the library is a few micro seconds faster at runtime / at load time.

    The reason is probably rather a psychological one than a real performance issue. -- I'd like to write clean code, use the ideal way and don't want to see warnings when running i.e. clazy on my code. When looking at performance there are probably other issues that have a greater impact on the performance.

    I actually tested it now:

    Test 1: Runtime usage #include <QString> const char *xmlns = "urn:ietf:xml:ns:xmpp-sasl"; // OR: const QString xmlns = QStringLiteral("urn:ietf:xml:ns:xmpp-sasl"); int main() { for (int i = 0; i < 1e9; i++) { // Option A: QString text(xmlns); // Option B: appending 'a' QString xmlnsPlusA = QString(xmlns) + 'a'; } } QStringLiteral char* A: QString( ), QStringLiteral is implicitly-shared 0.5 s 52 s B: appending 'a' 27 s 91 s

    -> The relative difference is high, but the absolute numbers are not relevant: You need to convert about 500 kB (about 20k comparable strings), so that this takes more than 1 ms and gets a performance problem in a GUI application.

    Test 2: Start-up time

    I also created a test binary with 10k global static constants (all with the same content):

    QString (cast from char *) QStringLiteral char * compile time 3 s 3.5 min < 1 s binary size (stripped) 410 kB 1270 kB 320 kB time to run (10k times) 33 s 23 s 19 s

    I guess the QStringLiteral binary takes longer to run, because it is larger and only partially, because of the non-POD CTORs.

    -> Using QStringLiteral has a slightly larger impact on the execution time than I've expected, but this is still not relevant since you usually have less than 10k global static strings and you only load the binary once instead of 10000 times. As long as you're not developing for an arduino this has probably no relevance.

    As expected the usage of QStringLiteral makes the binary a bit larger, the start-up time a little little bit larger, but is faster compared to casting from ascii everytime at runtime.

    I'll see if I can do anything with QArrayData, not because it's important, just for fun.

  • 0 Votes
    3 Posts
    3k Views
    SGaistS

    Hi,

    There's no need for any global objects all the more if you only have one active connection for your application but it's also valid if you are managing multiple connections. The QSqlDatabase class already provides everything you need.

    Like written in the documentation, if you are only using one connection, don't give it a name, it will become the default connection and all the SQL related classes will use it by default.

    If you have several connections, it's easy to retrieve the one you are interested in by using QSqlDatabase::database.

    On a side note, you should properly test the outcome of your open call. Right now it could be failing and you don't care about that possibility.

  • 0 Votes
    3 Posts
    920 Views
    M

    @raven-worx I will look into platform specific code. Thank you.

  • 0 Votes
    4 Posts
    1k Views
    S

    The singleton is part of the software design patterns.

    The implementation is easy: make the ctor private and implement one public static function which creates the instance:

    class Foo { private: static Foo* _instance; Test(); public: static Foo* getInstance() { if (_instance==NULL) _instance = new Foo(); return _instance; }

    cpp:

    Foo::_instance = NULL;

    And, please, put not all 'global variables' into one place, but consider the SOLID principles ;)

  • 0 Votes
    2 Posts
    2k Views
    A

    This seems a misunderstanding of language basics.
    Global and extern meaning are different.

    I would highly advice reading the book.

    But briefly:
    In such context extern means that you want to access variable _corner which is declared and instantiated in other module.
    Since it is not true you get a link error.

    At the same time I would highly advice against global variables in C++.
    Using such leads to code which is highly difficult to maintain.
    If you really need something accessible globally you may either subclass QApplication or use singleton pattern
    or at least use static class members .