c++

Safely checking the type of a variable

For a system I need to convert a pointer to a long then the long back to the pointer type. As you can guess this is very unsafe. What I wanted to do is use dynamic_cast to do the conversion so if I mixed them I'll get a null pointer. This page says http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=/com.ibm.vacpp7l.d...

std::auto_ptr or boost::shared_ptr for pImpl idiom?

When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of a std::auto_ptr? I'm sure I once read that the boost version is more exception friendly? class Foo { public: Foo(); private: struct impl; std::auto_ptr<impl> impl_; }; class Foo { public: Foo(); private: struct impl; boost::shared_...

Changing default behavior in a C++ application with plugins

In short: what is the best way to design and implement a factory+plugin mechanism, so that plugins can replace objects in the main application. We have a code base from which we build our applications. The code base is fine for 70-95% of the applications, meaning that in each new application we need to change 5-30% of the default behavi...

Fast container for setting bits in a sparse domain, and iterating (C++)?

I need a fast container with only two operations. Inserting keys on from a very sparse domain (all 32bit integers, and approx. 100 are set at a given time), and iterating over the inserted keys. It should deal with a lot of insertions which hit the same entries (like, 500k, but only 100 different ones). Currently, I'm using a std::set (...

check if X is derived of Y via typeid

i need to convert pointers to long (SendMessage()) and i want to safely check if the variable is correct on the otherside. So i was thinking of doing dynamic_cast but that wont work on classes that are not virtual. Then i thought of doing typeid but that will work until i pass a derived var as its base. Is there any way to check if the ...

Access desktop release diretory from windows ce device

I am writing a test program to copy some file in Applicaiton data folder on device to release directory on desktop. I am not sure how to access release dir on desktop? Is there any shell command to do that? Basically I want to write c++ program that will run on device to accomplish this task. ...

What do 'statically linked' and 'dynamically linked' mean?

I often hear the terms 'statically linked' and 'dynamically linked', often in reference to code written in C(++|#) but I don't know much of anything about either, what are they, what exactly are they talking about, and what are they linking? ...

Unit testing destructors?

Is there any good way to unit test destructors? Like say I have a class like this (contrived) example: class X { private: int *x; public: X() { x = new int; } ~X() { delete x; } int *getX() {return x;} const int *getX() const {return x;} }; Is there any good way to unit test th...

Redirecting cout to a console in windows

I have an application which is a relatively old. Through some minor changes, it builds nearly perfectly with Visual C++ 2008. One thing that I've noticed is that my "debug console" isn't quite working right. Basically in the past, I've use AllocConsole() to create a console for my debug output to go to. Then I would use freopen to redire...

Dot product in C++ using generic algorithms

I´m sure there´s a clever one-liner using the C++ stl generic algorithms for implementing the dot product of the elements in any ordered container, such as a vector or list. I just don´t seem to remember it! The fancy implementation would be: template <class containerT> typename containerT::value_type dot_product (const containerT& lef...

C++ Linking Errors: Undefined symbols using a templated class.

I'm getting some really wierd linking errors from a class I wrote. I am completely unable to find anything that will describe what is happening. Visual Studio (Windows XP) players.obj : error LNK2019: unresolved external symbol "public: __thiscall TreeNode::TreeNode(void)" (??0?$TreeNode@VPlayer@@@@QAE@XZ) referenced in function "p...

C++ array size dependent on function parameter causes compile errors

I have a simple function in which an array is declared with size depending on the parameter which is int. void f(int n){ char a[n]; }; int main() { return 0; } This piece of code compiles fine on GNU C++, but not on MSVC 2005. I get the following compilation errors: .\main.cpp(4) : error C2057: e...

kbhit() in mac

hi ppl. I am new to cpp in mac. I got error when I used kbhit() in my program. I used #include but got error too, so I searched and test with #include but error is still remained. so plz help me. thanks in advance. ...

Open source simple C++ with wxWidgets GUI applications

Hi, I would like to write a simple GUI application using C++ and wxWidgets. I'm wondering if there are any open source GUI applications using C++ and wxWidgets and tutorials for reference. Thanks, ...

What are the "string", "stream" and "stringstream" classes in C++?

I want to know what's the difference between string and stream in c++, and what's stringstream? ...

Returning a pointer from a class.

For my programming class I have to write a linked list class. One of the functions we have to include is next(). This function would return the memory address of the next element in the list. #include <iostream> using namespace std; class Set { private: int num; Set *nextval; bool empty; public: Set(); ...

Preventing stack overflows in C++ using arrays?

If we implement a stack using arrays in C++, what is the best way to reduce the chance of an overflow condition? Also while keeping in mind the time-space trade off? ...

What are some reasons a Release build would run differently than a Debug build

I have a Visual Studio 2005 C++ program that runs differently in Release mode than it does in Debug mode. In release mode, there's an (apparent) intermittent crash occurring. In debug mode, it doesn't crash. What are some reasons that a Release build would work differently than a Debug build? It's also worth mentioning my program is ...

What are some of the drawbacks to using C-style strings?

I know that buffer overruns are one potential hazard to using C-style strings (char arrays). If I know my data will fit in my buffer, is it okay to use them anyway? Are there other drawbacks inherent to C-style strings that I need to be aware of? EDIT: Here's an example close to what I'm working on: char buffer[1024]; char * line = N...

Providing an iterator for the first element of a container of pairs

I have a container filled with pairs. I want to iterate in it using the STL generic algorithms (in my case it would be inner_product, but consider it as a generic problem). The algorithm I am using expects iterators first and last. Can I provide special iterators first and last that will iterate not on the pairs but on the first element ...