c++

BST preorder traversal and writting tree content to temporary array

I'm trying to write binary search tree's content to temporary array in order to use in main. However I'm not sure how to do it... I have tried something like this: void Book::preorder(TreeNode *ptr, Person &temp[], int x) { if(ptr!=NULL) { temp[x].name=ptr->item.name; x++; preorder(ptr->left, temp, x); preorder(ptr->right, te...

Why does the number of elements in an enumeration change the visibility of a private typedef in C++?

Sorry for the not very descriptive question title. I'm not very sure how to describe this, hopefully I can make it better later if someone can explain this to me. I was about to come on here and ask why the following example was working. It was exhibiting the behaviour I was hoping for but I wasn't sure why or whether it was standard or...

Is calling base class constructor always necessary in C++?

Suppose I have some class C, and I inherit from it and name this class D. Do I always have to call C's default constructor as in this example: class C { public: C() { ... } }; class D { public: D() : C() { ... } }; Note that C has only the default constructor. Do I have to call it from D? I couldn't figure out...

What do I do if constructor fails to allocate memory in C++?

I just came across a problem where the constructor of a class needs to allocate memory. So I happily wrote char *mem = static_cast<char*>(malloc(100*sizeof(*mem)));. But then I suddenly realized that in case of error I can't return error code (I am not using exceptions in my code). How can I solve this problem? Should I add an bool init...

Do access modifiers work for static class functions?

I just came across code that had protected static class functions, as in: class C { ... protected: static int fun() { ... } }; I got curious if static class functions could have access modifiers and what would it mean? Since they are class globals and not pre-instance. Thanks, Boda Cydo. ...

What are the most common uses of static class functions in C++?

I am learning today about static class functions in C++ and I can't really understand what are they good for? Does anyone have some good examples where they can be applied successfully? Thanks, Boda Cydo. ...

How does one rename a variable midway through a block?

In C++, how does one rename a variable midway through a block? References and macros feel wrong. ...

Can I throw C++ exceptions from legacy C callbacks?

I have C++ code that uses some C libraries. The C libraries take C language callbacks. I wrote a callback in my C++ code and now I somehow need to report error from it (but it returns void). I wonder if I can throw an exception from a C callback that is used from C++ code? This is very difficult for me to understand. Thanks, Boda Cydo....

segmentation fault when i access member variables using a callback function

I have a class, in which i declare a static function. I register this function as the callback function for another library. Now, inside this function, i dereference the callback data to the class pointer and invoke a particular non-static member. The invocation succeeds and the control comes into the non-static member function. But ins...

DIfference between macros and templates - compilation/build time

Macros are expanded by preprocessors and templates are by compilers. But, in terms of compilation/build time, which one takes longer? ...

What is the best free/paid C++ Video Training resource out there?

Hello, I'm a .NET Developer, primarily using c#, asp.net mvc, entity framework and programming for the Web. I want to know what you guys recommend to get a quick startup in learning c++. My biggest worries are pointers, memory management and other languages differences between managed and native languages. I want to know if there is some...

Templates, Circular Dependencies, Methods, oh my!

Background: I am working on a framework that generates C++ code based on an existing Java class model. For this reason I cannot change the circular dependency mentioned below. Given: A Parent-Child class relationship Parent contains a list of Children Users must be able to look up the list element type at run-time I've modeled this ...

Best way to concatenate two vectors?

I'm using multitreading and want to merge the results. ex: std::vector<int> A; std::vector<int> B; std::vector<int> AB; I want AB to have to contents of A and the contents of B in that order. What's the most efficient way of doing something like this? Thanks ...

Generating word documents (.doc/.odt) through C++/Qt

Hi, I am using Qt 4.5.3 and Windows XP. I need my application to generate documents that contains the information that is being used and generated. The information that is being used will be just strings (QString to be more specific) and the information that is being generated will be strings and images as well. I want documents to be ...

Is it safe for multiple threads to call the same function?

Is it safe to for example do: void AddTwo(int &num) { num +=2; } void ThreadProc(lpvoid arg) { AddTwo((int)arg); } Would it be safe for this to occur if 4 threads were doing this at the same time? Thanks ...

set GLOBAL clipboard text in windows, native c++

I have an ASCII string (A null terminated char array) in a console app. all I want to do is make it so my app will put this string into the "global clipboard" so that after running it, I can ctrl+v in any standard app (in this case, visual studio) and my string will be pasted! how do I do this? I have done: void SetClipboardText(char...

MS Word/ ODF Automation in Qt

Hi All, How can I perform automation of MS Word documents (.doc) or ODF documents (.odt) in Qt 4.5? I know using the QAxWidget, QAxObject. I have data (QString) and few images as well. I have to add them into the document. I googled but I couldn't find any commands for MS- Word/ ODF. But I want the specific commands that should be pas...

C/C++: How to figure out the chain of header files for a given definition?

In Visual C++, one can find the header file where any name (variable or type) is defined by pressing F12 on it or choosing Go to Definition. This feature is very useful, but it only shows the final location (header file) where the name is defined. Is there a way to figure out the chain of header files that lead from my source file to the...

how to implement is_pointer ?

Hi all. I want to implement is_pointer. I want something like this: template <typename T > bool is_pointer( T t ) { // implementation } // return true or false int a; char *c; SomeClass sc; someAnotherClass *sac; is_pointer( a ); // return false is_pointer( c ); // return true is_pointer( sc ); // return false is_pointer( sac );...

why append Slot doesn't work?

Hi all, I have got a problem when I try to make following simple connections QSpinBox *spinBox = new QSpinBox; QSlider *slider = new QSlider(Qt::Horizontal); QTextEdit *text = new QTextEdit("Hello QT!"); QObject::connect(spinBox, SIGNAL(valueChanged(int)),slider, SLOT(setValue(int))); QObject::connect(slider, SIGNAL(valueChanged(in...