c++

Practice of having a "Common" header

By common I don't mean utility, I mean a header that holds enums that multiple types want to use, etc. For example, if multiple types can have a Color, which is an enum, you'd want to make that available. Some people would say to put it into the class that it "fits best with", but this can create header dependency issues. I really disl...

How to get at TCP RTT on Windows (Linux TCP_INFO) as an user

I am porting a streaming TCP app from Linux to Windows. The app streams real-time audio data using a preexisting TCP protocol (so switching to UDP isn't an option). Further, I wish to avoid being "part of the problem" and requiring Administrator rights. The Linux code uses getsockopt(... ,SOL_TCP, TCP_INFO, ..) to get the RTT (roun...

Posixy way to launch browser?

Is there a 'Posixy' way to open an URL, preferrably in the default browser? I would like to do something like ShellExecute(0, _T("open"), url, 0, 0, SW_SHOWDEFAULT); that works on GNU/Linux and MAC. I read some answer saying that` if (fork() == 0) system("sensible-browser http://wherever.com"); does the trick on Debian systems ...

what can I use to replace sleep and usleep in my Qt app?

I'm importing a portion of existing code into my Qt app and noticed a sleep function in there. Now after doing some researching I read that this type of function has no place in event programming. What should I do instead? Also, I am aware of QThread::sleep() [static protected] and subclassing it but I heard thats a hack and ends up d...

C++: When is it approperiate to use multiple inheritance (when is is inapropriate and used)

Possible Duplicate: A use for multiple inheritance ? How is multiple inheritance really a useful construct in C++. Why use it? What are examples of where it can solve problems that can not be solved in some other clean way? What are some examples of how to use it in a useful way? How is it misused? What are the problems wi...

How can I add an item to two queues and guarantee that it exists in both or none (multi-threaded)

I have the following problem. I have two classes, in this case A and B, which both own a concurrent_queue. The assumption here is that concurrent_queue is a thread-safe queue, with a blocking push() function. When an object is enqueued in B, it accesses the singleton A and it is queued up in A as well. This has the effect that a whole b...

Should you rely on another header for the headers it includes?

Assuming that all headers are guarded, let's say you had an abstract data type. #include "that.h" #include "there.h" class Foo { protected: // Functions that do stuff with varOne and varTwo private: that varOne; there varTwo; ... }; Then in the classes that inherit from foo ( and thus include foo.h ), would you also bother ...

Thread-safe timezone-specific time display in C/C++

I have a multi-threaded application which needs to display certain dates to the user. The dates are stored using UTC Unix time values. However, the date must be displayed in the time zone of the user, not the local server time or UTC. Basically, I need a function like this: struct tm *usertime_r(const time_t *timer, struct tm *result, c...

How do I create and use a CFormView in an MFC regular DLL? (visual studio 2008)

I recently asked this question which got me started in the right direction - at least for loading the MFC DLL and trying to show a dlg box. The problem is, the typical dialog box is horrible as a main window for an APP. It is quite simple for me to create a new exe project to do what I want, but the problem is that I have a DLL and t...

C++ design question (need cheap smart pointer)

I have a huge tree where keys insides nodes are indices into a big hash_map v, where v[key] is a (big) record associated with that key (includes how many nodes in the tree have this key). Right now, key is an integer. So each node has overhead of storing pointers for children and an integer. We can remove a key from a node in the tree. W...

Is there any way to find the address of a reference?

Is there any way to find the address of a reference? Makingit more specific: The address of the variable itself and not the address of the variable it is initialized with ...

C++ Function Overloading Similar Conversions

I'm getting an error which says that two overloads have similar conversions. I tried too many things but none helped. Here is that piece of code CString GetInput(int numberOfInput, BOOL clearBuffer = FALSE, UINT timeout = INPUT_TIMEOUT); CString GetInput(int numberOfInput, string szTerminationPattern, BOOL clearBuffer = FALSE, UINT tim...

How do I get the position of a control relative to the window's client rect?

I want to be able to write code like this: HWND hwnd = <the hwnd of a button in a window>; int positionX; int positionY; GetWindowPos(hwnd, &positionX, &positionY); SetWindowPos(hwnd, 0, positionX, positionY, 0, 0, SWP_NOZORDER | SWP_NOSIZE); And have it do nothing. However, I can't work out how to write a GetWindowPos() function that...

what is the difference when the number of threads is determined and undetermined?

Hi, what the difference when the number of threads is determined, as e.g.: for (i*10){ ... pthread_create(&thread[i], NULL, ThreadMain[i], (void *) xxx); ... } and when it is undetermined, just like this: ... pthread_create(&threadID, NULL, ThreadMain, (void *) xxx); ... In my case the numb...

How can I check the failure in constructor() without using exceptions ?

All of the classes that I'm working on have Create()/Destroy() ( or Initialize()/Finalized() ) methods. The return value of the Create() method is bool like below. bool MyClass::Create(...); So I can check whether initialization of the instance is successful or not from the return value. Without Create()/Destroy() I can do the same j...

C: Where is union practically used?

I have a example with me where in which the alignment of a type is guaranteed, union max_align . I am looking for a even simpler example in which union is used practically, to explain my friend. ...

g++: warning: integer constant is too large for ‘long’ type

What can I do (programmatically) to get rid of the warning? ... unsigned long long v=(unsigned long long)0xffffeeeeddddcccc; ... g++ main.cpp -o main main.cpp:6: warning: integer constant is too large for ‘long’ type but when I run the program everything is fine as expected: ./main sizeof(unsigned long long)==8 value of v==0x...

When to use std::size_t?

Hi, I'm just wondering should I use std::size_t for loops and stuff instead of int? For instance: #include <cstdint> int main() { for (std::size_t i = 0; i < 10; ++i) // std::size_t OK here? Or should I use, say, unsigned int instead? } In general, what is the the best practice regarding when to use std::size_t? ...

C++ Function clarification

Given: x = MyFunc(2); My understanding: The variable x is assigned to the function MyFunc(2). First, MyFunc( ) is called. When it returns, its return value if any, is assigned to x.? ...

C++ code runs with missing header, why?

I just realized that I am supposed to include the #include<cstdlib> required by abs() for the abs() function. #include<iostream> using namespace std; int main() { int result; result = abs(-10); cout << result << "\n"; return 0; } Why does this code still work, even though I forgot the ...