c++

Boost asio ip tcp iostream Error Detection

Greetings. I'm just getting started with the boost::asio library and have run into some early difficulty related to boost::asio::ip::tcp::iostream. My question has two parts: 1.) How does one connect an iostream using simply host and port number? I can make the client and server [boost.org] examples work fine as coded. The server sp...

Why do some languages need Boxing and Unboxing ?

Hi, This is not a question of what is boxing and unboxing, it is rather why do languages like Java and C# need that ? I am greatly familiar wtih C++, STL and Boost. In C++ I could write something like this very easily, std::vector<double> dummy; I have some experience with Java, but I was really surprised because I had to write som...

Any way to get the AtomineerUtils addin to ignore __declspecs?

I'm evaluating the AtomineerUtils addin (which allows Visual Studio to auto-enter doxygen comment blocks). It misfires slightly when I have a class definition that has __declspec (dllexport) in it. That is, instead of the class name appearing in the comment block it adds, it uses the word declspec. Small annoyance, but annoyance nonet...

how to Ignore definitions (VS2008)

Hi, i have some sourcecode that I want to compile with VS2008 but there are many errors i have to fix. Now there are some Enums like: enum { BACKGROUND = 0x00000001, WEAPON = 0x00000002, TRANSPARENT = 0x00000004 } The problem is that TRANSPARENT is defined as: #define TRANSPARENT 1 in WinGDI.h That will cause a comp...

Reference-type conversion operators: asking for trouble?

When I compile the following code using g++ class A {}; void foo(A&) {} int main() { foo(A()); return 0; } I get the following error messages: > g++ test.cpp -o test test.cpp: In function ‘int main()’: test.cpp:10: error: invalid initialization of non-const reference of type ‘A&’ from a temporary of type ‘A’ test.cpp:6: er...

throwing exceptions of objects on the stack, mem leak with new?

Is it a bug to do this: if(some_error) throw Cat("Minoo"); Where Cat is a class. Then in some other function that called the method that threw the exception... I would have: catch(const Cat &c) { } If it is invalid, do I use new Cat("Minoo"); Would that cause a memory leak? ...

Calling C++ function from C#

Hello, I have a the following C++ function void FillAndReturnString(char ** someString) { char sourceString[] = "test"; *someString = new char[5]; memcpy(*someString, sourceString, 5); } It is declared as extern "C" { __declspec(dllexport) void __cdecl FillAndReturnString(char ** someString); } How do I call this func...

Rethrowing from caught ...

If I throw an exception: throw Cat("Minoo"); Then I catch and rethrow with ... at some lower level in the call stack: catch(...) { throw; } Then at some other lower level in the call stack I try to catch with: catch(const Cat& c) { //Will it enter here, and if so will c be valid data? } catch(...) { } ...

Error instatiating COM object

Hi all, currently I'm struggling trying to use a COM dll on a simple system that I've made. Everything compiles successfully, but in runtime the CoCreateInstace is returning S_OK, but somehow my object pointer is returning NULL. This interface pointer is created on my class header. The weirdest thing is that instantiating this same po...

The "Self-Factory" Pattern

I don't know if there is an official name for this, but I have been playing with what I like to call the "self-factory" pattern. Basically, it's when an abstract base class acts as a factory for itself. Let me explain: I have Foo objects and Bar objects in my system, which are used via interfaces FooInterface and BarInterface. I need...

Fluent interfaces and inheritance in C++

I'd like to build a base (abstract) class (let's call it type::base) with some common funcionality and a fluent interface, the problem I'm facing is the return type of all those methods class base { public: base(); virtual ~base(); base& with_foo(); base& with_bar(); protected: // whatever.....

When is it appropriate to use "delete this"?

Possible Duplicate: Should objects delete themselves in C++? In my application, I'm creating many objects that "own themselves" - in the sense that after they are created and told to "go," only the object itself can determine when it should be deleted. For example, if I was writing a game, I might have multiple Enemy objects. ...

is using *this a good idea?

Hi, I'm not sure if return *this is the only way we could return an instance of a class who called a member function? Reason why I asked is because our instructor told us to avoid using pointers if necessary and I'm wondering if this is a case where the only necessary way to do it is by returning the this pointer. I'm working wit...

Most efficient way to erase duplicates and sort a c++ vector?

I need to take a C++ vector with potentially a lot of elements, erase duplicates, and sort it. Looks like this code will do the trick: (Correction--it won't; next time I'll test before posting. Thanks for the feedback.) vec.erase( std::unique(vec.begin(), vec.end()), vec.end()); std::sort(vec.begin(), vec.end()); Is it f...

extern "C"

Hello, Stack Overflow! I was wondering what exactly putting 'extern "C"' in your C++ program does. Thanks! ...

Speeding up non-blocking Unix Sockets (C++)

I've implemented a simple socket wrapper class. It includes a non-blocking function: void Socket::set_non_blocking(const bool b) { mNonBlocking = b; // class member for reference elsewhere int opts = fcntl(m_sock, F_GETFL); if(opts < 0) return; if(b) opts |= O_NONBLOCK; else opts &= ~O_NONBLOCK; ...

Compiler not flagging incorrect return value for HRESULT

I just spent way too long trying to diagnose why, in the following snippet of code, the ProcessEvent() method seemed to be ignoring the false value I passed in for aInvokeEventHandler: HRESULT CEventManager:: Process(Event anEvent) { return (m_pPool->GetFsm()->ProcessEvent(anEvent), false); } // Definition of ProcessEvent() HRESUL...

Using scanf() in C++ programs is faster than using cin ?

Hello, I don't know if this is true, but when I was reading FAQ on one of the problem providing sites, I found something, that poke my attention: Check your input/output methods. In C++, using cin and cout is too slow. Use these, and you will guarantee not being able to solve any problem with a decent amount of input or output. Use...

Issues Serial Port Watch using threads with an event loop and QSocketNotifiers

Hi all, I asked this question yesterday since I wasn't receiving any data but strangely when I used wait in the destructor I started receveing notification from QSocketNotifier. The rest of the question is same. Can someone suggest something? I have created a sample application from where separate thread is started to read and process d...

Literate Coding Vs. std::pair, solutions?

As most programmers I admire and try to follow the principles of Literate programming, but in C++ I routinely find myself using std::pair, for a gazillion common tasks. But std::pair is, IMHO, a vile enemy of literate programming... My point is when I come back to code I've written a day or two ago, and I see manipulations of a std::pai...