Skip to content

C++ Gurus

The forum for all discussions in C++ land.
1.3k Topics 8.5k Posts
QtWS25 Last Chance
  • The Singleton Pattern

    15
    0 Votes
    15 Posts
    19k Views
    G
    See "C++ FAQ 16.8":http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.8: bq. C++ language guarantees that delete p will do nothing if p is equal to NULL. Since you might get the test backwards, and since most testing methodologies force you to explicitly test every branch point, you should not put in the redundant if test. It is guaranteed by the standard that assigning 0 (digit zero) to a pointer in the source code is equivalent to assigning null to the pointer (the same holds for comparison with ==). This is independent of the internal representation of a null pointer! The latter can be different from the numeric value zero, so memsetting all bits of a pointer to zero can lead to something different than a null pointer!
  • What Every C/C++ Programmer Should Know About Undefined Behavior

    3
    0 Votes
    3 Posts
    3k Views
    A
    Thank you for sharing!
  • Displaying image using label in Qt4.7

    6
    0 Votes
    6 Posts
    14k Views
    J
    Thanks for yours response. I just found my mistake. I should have put clicked() instead of triggered(). Thanks a lot
  • Need to swap row in a QTableWidget

    3
    0 Votes
    3 Posts
    7k Views
    P
    Thanks a lot, I use you model function to take the model, and I sort a column in this model. So the column represent the index and I just need to swap the index value in column and I use sort function.
  • Function pointers in Qt

    30
    0 Votes
    30 Posts
    27k Views
    G
    Good night :-) Please regard the updates I made to the code
  • My sub-sub-class is not being included

    15
    0 Votes
    15 Posts
    5k Views
    A
    For Linux users: I kept on having problems when attempting to run the application built in QtCreator, I tried the -R option but it didn't work. In the end I found out somewhere on internet that I should create a myLibs.conf file inside /etc/ld.so.conf.d with the path to my libraries. That did the trick, fyi. Boris
  • [Solved] Problem defining a std::endl manipulator for a custom class

    6
    0 Votes
    6 Posts
    4k Views
    K
    Thanks for clarification. The naming is a little misleading then. That made me stumbling when trying to understand. Anyhow, I am happy that your problem has been solved.
  • [Solved]Cannot define structure at the same time

    6
    0 Votes
    6 Posts
    5k Views
    P
    Yes, it works fine now, thanks.
  • 0 Votes
    15 Posts
    6k Views
    F
    You can of course do something like @ typedef struct Object { void (*setData)(struct Object *, int); int (*data)(struct Object *); int m_data; } Object; Object *objectConstructor() { Object *o = malloc(sizeof(Object)); memset(o, 0, sizeof(Object)); o->setData = objectSetData; o->data = objectData return o; } void objectSetData(Object *this, int data) { this->m_data = data; } int objectData(Object *this) { return this->m_data; } @ But that would arguably obscure your code for the simple fact that you have to explicitly pass the this argument: @ Object *o = objectConstructor(); o->setData(o, 5); printf("%d\n", o->data(o)); @ thereby making the function pointers look rather awkward.
  • More data help

    16
    0 Votes
    16 Posts
    7k Views
    J
    [quote author="Tobias Hunger" date="1303314614"]I'd just decide on one byte order to use in the file and stick with that. That avoids the bigendian bool and the if/else. Branches are always slow and avoiding them in a method that is called all the time is a good idea: Extracting ints from a data file smells like something that will happen rather often. Why do you need the data.mid()? That constructs a temporary object that is not necessary. @data.constData() + offset@ should work just as well as @data.mid(offset, 2).data()@ without creating a temporary object. I'd also try to use constData whenever possible. Using data() the way you do the compiler might think it needs to copy the temporary object you created since somebody might want to write into those bytes. Using constData is a very clear hint that this is not going to happen and the compiler can optimize the copy out. Of course it might be intelligent enough to do it for the data() case, too, but who wants to bet on the compiler being intelligent? ;-) [/quote] I have no control over the file and it has both bigendian and littleendian values (just to make my life hard) as it was a pc game that was ported to xbox 360 :s Also you're 100% right I'll use the data.constData() + offset as I have no real need for a temp object :) thanks again.
  • Question about binary data sotrage.

    7
    0 Votes
    7 Posts
    3k Views
    J
    I will thanks :D
  • [solved] Statement always null

    2
    0 Votes
    2 Posts
    2k Views
    A
    Because you are dividing integers. If you want to have a floating point result, you need to cast to a floating point number before dividing: @ double newPos = static_cast<double>(ev->x())/90.0; @
  • Data variable scope

    4
    0 Votes
    4 Posts
    2k Views
    Z
    I would be tempted to use the singleton pattern for your top-level list. It is quick and easy to do and is easy to refactor if you decide to ever provide a non-gui version of your app.
  • [Moved] system() not running as expected

    5
    0 Votes
    5 Posts
    3k Views
    G
    "QProcess":http://doc.qt.nokia.com/4.7/qprocess.html should be a good alternative. You have control over stdin and stdout there.
  • Is Implicit Sharing always a good thing?

    14
    0 Votes
    14 Posts
    7k Views
    D
    [quote author="Gerolf" date="1301636313"]but the checks are cheap, compared to deep copying stuff.[/quote] Of course -- my point was that you have no control upon them and you pay for them even if your container is not shared at all. The same thing, mutatis mutandis, applies for any implicitly shared class. [quote]Having spent many years programming for hand held devices, dealing with low and no memory was always high on the list of the way we programmed. And so was important and was effectively dealt with in many/most situations. Do you really think it is acceptable for for a word processor or spreadsheet to crash due to low memory and loose sever hours of the users work? It is very difficult to add this sort of robustness to code after you have written it – it has to be done right from the start[/quote] You're absolutely right and I wasn't disagreeing with you. My point was: when a OOM happens, there's almost nothing you can safely do except from crashing the application. Anything you do is at risk of failing again because it tries to allocate more memory.
  • [Solved] Basic QIODevice subclass in Qt4

    49
    0 Votes
    49 Posts
    31k Views
    G
    also correct and a good idea. What you do not capture with this, is errors of storing itself.
  • Windoes Service Status &#63;

    6
    0 Votes
    6 Posts
    5k Views
    R
    Thanks alot, i have it. This is the working code. @LPCWSTR ServiceName = L"My ServiceName"; SC_HANDLE hScManager = OpenSCManager(0, // local computer or add computer name here 0, // SERVICES_ACTIVE_DATABASE database is opened by default. GENERIC_READ); // onyl read info if(0 != hScManager) { SC_HANDLE hSvc = OpenService(hScManager, // service manager ServiceName, // service name GENERIC_READ); // onyl read info if(0 != hSvc) { SERVICE_STATUS_PROCESS sInfo; DWORD bytesNeeded = 0; if(QueryServiceStatusEx(hSvc, // A handle to the service. SC_STATUS_PROCESS_INFO, // info requested (LPBYTE)&sInfo, // structure to load info to sizeof(sInfo), // size of the buffer &bytesNeeded)) { if(sInfo.dwCurrentState == SERVICE_RUNNING) { traymessage("test", "start!!!", 3); } else { traymessage("test", "stop!!!", 3); } } } }@
  • Bits manipulating question

    7
    0 Votes
    7 Posts
    8k Views
    F
    To control bits, you can use: | (binary or ) => sets bit & with negative mask => unset bit not that negative mask can be done using ~ (binary inversion) : result = result & (~BIT4) <<< unsets bit 4
  • [Moved] A question about pointers.

    14
    0 Votes
    14 Posts
    9k Views
    T
    [quote author="Volker" date="1300533129"][quote author="Taamalus" date="1300504867"]^ This may not work. http://www.codeguru.com/forum/archive/index.php/t-358371.html [/quote] What's the rationale behind this? To my knowledge even in poor old C (without ++) "0" is the all-valid constant for a null pointer (regardless of the internal representation for the actual machine). So @ if(ptr == 0) doFancyThings(); @ is always valid code (See "comp.lang.c FAQ list, Question 5.2":http://c-faq.com/null/null2.html and "Question 5.5":http://c-faq.com/null/machnon0.html). [/quote] Thanks, I stand corrected. :) Still, there is no way I will use it, but from now on, just as a personal preference. http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer this link is just to save face :D on my preferences Also thanks Gerolf! Re: Null Object Patterns, not for C++ but for LISP! Cheers! :)
  • Strange Behavior with pointers

    4
    0 Votes
    4 Posts
    3k Views
    G
    so, this is C++ basics: @ PathNode* currNode; currNode = new PathNode(*node, 0.0, 0.0); @ currNode is the pointer. @ *currNode; @ dereferences the pointer and returns a PathNode object (reference). @ &currNode; @ gives you the address of the pointer, so a PathNode** If you want to store the pointer in the map, use the pointer as in the list: @ PathNode* currNode; currNode = new PathNode(*node, 0.0, 0.0); m_bhOpenList.Push(currNode); m_mapMasterList.insert(start, currNode); @