c++

Does C++ deep-initialize class members?

I have a class in C++ with the following member: map< someEnum, vector<SomeObject*>* > someMap So I have a map that gives me a vector of objects for each enumeration I have. For the life of me, I cannot understand how C++ is initializing these objects. Does it deep initialize them by default? If not, what do I need to do? I'm getting...

Is there any way to determine how many characters will be written by sprintf?

I'm working in C++. I want to write a potentially very long formatted string using sprintf (specifically a secure counted version like _snprintf_s, but the idea is the same). The approximate length is unknown at compile time so I'll have to use some dynamically allocated memory rather than relying on a big static buffer. Is there any ...

Best way to return list of objects in C++?

It's been a while since I programmed in C++, and after coming from python, I feel soooo in a straight jacket, ok I'm not gonna rant. I have a couple of functions that act as "pipes", accepting a list as input, returning another list as output (based on the input), this is in concept, but in practice, I'm using std::vector to represent...

calling base class functions

I have following classes. class A { public: void fun(); } class B: public A { } class C: public A { } A * ptr = new C; Is it ok to do something like below? Will i have some problems if introduce some virtual functions in the baseclass? ((B *)ptr)->fun(); This may look stupid, but i have a function that calls A's function th...

Suggestion for template book for C++?

I am learning templates. Which book is worth buying for doing template programming? I already have The C++ Programming Language and Effective C++. ...

Destructor called on object when adding it to std::list

I have a Foo object, and a std::list holding instances of it. My problem is that when I add a new instance to the list, it first calls the ctor but then also the dtor. And then the dtor on another instance (according to the this pointer). A single instance is added to the list but since its dtor (along with its parents) is called, the o...

Why does GCC look at private constructors when matching functions?

I'm very busy write now debugging some code, so I can't cookup a complete example, but this basically describes my problem class Base{}; class MyX:public Base { ... }; class Derived:Base { ... }; template<class X> class MyClass:Derived { private: MyClass(const MyClass& ) :x() {} public: MyClass(const X& value) :x(...

Try/Catch a segmentation fault on Linux

I have a Linux C++ application and I'd like to test an object pointer for validity before dereferencing it. However try/catch doesn't work for this on Linux because of the segmentation fault. How can this be done? ...

How to set up headers and libraries for Linux development.

I recently set up, for a learning exercise, an Ubuntu desktop PC with KDE 4.2, installed Eclipse and started to look for information on how to develop for KDE. I know there's KDevelop and will probably have a look at that at some time in the future. Right now, however, I don't have the correct headers and libraries for creating KDE appli...

Do I need to lock STL list with mutex in push_back pop_front scenario?

I have a thread push-backing to STL list and another thread pop-fronting from the list. Do I need to lock the list with mutex in such case? ...

How can Derived class inherit a static function from Base class?

struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void *Arg); }; HandleTimer needs to be static since I'm passing it to C library (libevent). I want to inherit from this class. How can this be done? Thanks. ...

Can I stop C++/CLI from adding IDisposable to my ref class?

C++/CLI helpfully generates the IDisposable scaffolding for you when you implement a destructor on a ref class. Also, if you don't implement a destructor, but your class has a member variable which implements IDisposable, then IDisposable is again automatically implemented on your class. It's pretty helpful and much better than how IDisp...

Deduction of reference types in template functions

Do I have to explicitly instantiate a function template's type when it comes to reference type deduction. If that is the case, where is the ambiguity? Let's compare following 2 code snippets: 1st: link for the code template <typename T> void foo(T& var, void(*func)(T&)) // T must be instantiated with int and it does . { ++var; } vo...

std::map, pointer to map key value, is this possible?

std::map<std::string, std::string> myMap; std::map<std::string, std::string>::iterator i = m_myMap.find(some_key_string); if(i == m_imagesMap.end()) return NULL; string *p = &i->first; Is the last line valid? I want to store this pointer p somewhere else, will it be valid for the whole program life? But what will happen if I ad...

Relative Paths Not Working in Xcode C++

There are numerous post over the net that detail how relative paths don't work in Xcode. I do have an Xcode template that I downloaded where the relative paths DO work, however I have not been able to figure out why nor replicate it in other projects. Firstly, I am using C++ in Xcode 3.1. I am not using Objective-C, nor any Cocoa/Carbon...

When should I use typedef in C++?

In my years of C++ (MFC) programming in I never felt the need to use typedef, so I don't really know what is it used for. Where should I use it? Are there any real situations where the use of typedef is preferred? Or is this really more a C-specific keyword? ...

When don't I need a typedef?

I encountered some code reading typedef enum eEnum { c1, c2 } tagEnum; typedef struct { int i; double d; } tagMyStruct; I heard rumours that these constructs date from C. In C++ you can easily write enum eEnum { c1, c2 }; struct MyStruct { int i; double d; }; Is that true? When do you need the first variant? ...

raw function pointer from a bound method

I need to bind a method into a function-callback, except this snippet is not legal as discussed in demote-boostfunction-to-a-plain-function-pointer. What's the simplest way to get this behavior? struct C { void m(int x) { (void) x; _asm int 3; }}; typedef void (*cb_t)(int); int main() { C c; boost::function<void (int x...

Visual Studio Debuging Errors in C++

For some reason the integrated debugger is causing an error as soon as I make reference to a third party vendor's dll class. This same code runs when it is built and ran as a release, stand alone. The two properties for debug and release should be the same as I have not really altered them. I added the lib file to the path for both build...

Why can't I put an iterator in map?

I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string named "mHugeString". Then I walk trough the string collecting iterators like this: std::string::iterator It=mHugeString.begin(); std::string::iterator EndIt=mHugeString.end(); for(;It!=EndIt;++It){ ...defining a key element...