c++

C++ "smart pointer" template that auto-converts to bare pointer but can't be explicitly deleted

I am working in a very large legacy C++ code base which shall remain nameless. Being a legacy code base, it passes raw pointers around all over the place. But we are gradually trying to modernize it and so there are some smart pointer templates as well. These smart pointers (unlike, say, Boost's scoped_ptr) have an implicit conversion...

Cleaner way to convert from CStringW to std::string?

I figured out a somewhat convoluted way to convert a CStringW to a std::string, but I was wondering if there's a cleaner way than: CStringW cwstr; std::wstring stdwstr = cwstr; std::string stdstr = CW2T(stdwstr.c_str()); ...

Unable to dispose of C# COM object

I have a C# class that I've made ComVisible so that it can be used in an unmanaged C++ DLL. The C# class is defined like this: public interface IFSFunction { double GetProcessTime(); } public class Functions : IFSFunction { // Initialization here // Interface function public double GetProcessTime() { // Do ...

c++ friend function - operator overloading istream >>

Hello all, My question is in regards to friend functions as well as overloading the << and >>. From my understanding I thought friend functions could (and should) access private member variables directly. However in the case I have here the compiler would only accept my .cxx file when I used "get" functions to obtain each private variab...

Use C++ constants in C# program

Hello All, We have a situation where we use a set of third-party unmanaged C++ libraries in our C# (WPF) application, but we also use a subset of their include libraries to build our own unmanaged libraries to use in our application. These libraries produce metadata, which is stored in a database. However, we must replicate some const...

How to print an object of unknown type

I have a templatized container class in C++ which is similar to a std::map (it's basically a thread-safe wrapper around the std::map). I'd like to write a member function which dumps information about the entries in the map. Obviously, however, I don't know the type of the objects in the map or their keys. The goal is to be able to ha...

On a disadvantage of exceptions in C++

I was reading Google C++ Style Guide, and got confused in the Exceptions part. One of the cons of using it, according to the guide is: Exception safety requires both RAII and different coding practices. Lots of supporting machinery is needed to make writing correct exception-safe code easy. Further, to avoid requiring reade...

Error in template function (using Boost.Tuples)

#include <list> #include <boost/tuple/tuple.hpp> template<class InputIterator> void f(InputIterator it) { typedef boost::tuple<typename InputIterator::value_type, int> Pair; std::list<Pair> paired; typename std::list<Pair>::const_iterator output; for(output=paired.begin(); output!=paired.end(); ++output) { ou...

Spatial Vector, Color Vector

Say you have two things (spatial vector, color vector) that are conceptually different (pos in space vs color) yet they actually end up needing the same types of operations - overloaded plus, minus, scalar multiply. A nice solution in the Cg programming language was they actually aliased member names .xyzw and .rgba. So in Cg you can d...

accessing the last 2 bytes of an integer

i have a struct that contains an integer and a char..how do i access the last 2 bytes of my int?? ...

Why do the characters ÌÌÌÌ get stuck onto the begining of my strings?

Hello I have been developing some computer vision tools with openCV, but every time that I pass a string into an openCV function, the characters ÌÌÌÌ get tagged onto the beginning. At first this was just annoying, but now I am trying to use openCV's fileStorage tools and the ÌÌÌÌ characters are making my file names unreadable. Note: th...

Mobile physics engine

Hello, I'm looking to use a physics engine for a 3D mobile game that I'm working on. I'd like to use a library that supports fixed point math and preferably coded in C++. Any recommendations? ...

GDB (DDD), debugging questions

Some things in GDB (actually using DDD gui) confuse me, when debugging my own C++ codes: 1) Why is there no backtrace available after a HEAP ERROR crash? 2) Why does gdb sometimes stop AFTER the breakpoint rather than AT the breakpoint? 3) Sometimes stepping through commented lines causes execution of some instructions (gdb busy)?? Any...

Possible compilation errors in C++

In C++, what kind of compilation errors might I run into while using function overloading, and when might these occur? ...

Linked list head double pointer passing

Hi, I have seen this in some book/ tutorial. When you pass in the head pointer (of linked list) into a function, you need to pass it as a double pointer. For eg: // This is to reverse a linked list where head points to first node. void nReverse(digit **head) { digit *prev=NULL; digit *curr=*head; digit *next; while(cu...

Width as a variable when using fscanf [C++]

I am trying to read in a certain portion of a file and that amount of data is different per line but I know how how many bytes of info I want. Like this: 5bytes.byte1byte2byte3byte4byte5CKSum //where # of bytes varies for each line (and there is no period only there for readability) Actual data: 05AABBCCDDEE11 03AABBCC22 04AABB...

Complexity of STL max_element

So according to the link here: http://www.cplusplus.com/reference/algorithm/max_element/ , the max_element function is O(n), apparently for all STL containers. Is this correct? Shouldn't it be O(log n) for a set (implemented as a binary tree)? On a somewhat related note, I've always used cplusplus.com for questions which are easier to a...

template self friendship

template < typename T > struct test { template < typename U > friend struct test<U>; }; int main() {} This is perfectly valid code, no? I ask because MSVC++ 2010 fails to compile it. Not the first time templates have confuzled the MS compiler though. As far as I can tell from books, websites, and such it should work. The corre...

When catching an exception's parameter by value, what is the order in which the destructors are called?

In the following code, what is the order in which the destructors of b, q and e are called, and which is called before handling the exception. (The "cout..." parts are leftover for the original question) #include <iostream> using namespace std; class A { public: A(int arg) : m(arg) { cout << "A::A(int) " << m << endl; m...

std::tuple get() member function

boost::tuple has a get() member function used like this: tuple<int, string, string> t(5, "foo", "bar"); cout << t.get<1>(); // outputs "foo" It seems the C++0x std::tuple does not have this member function, and you have to instead use the non-member function form: std::get<1>(t); which to me looks uglier. Is there any particular ...