c++

in which area is c++ mostly used?

I've been asked many times by my juniors about the areas in which C++ is widely used. I usually answer Operating Systems. Are there any other areas where its extensively used? ...

changing part of a file in C++

Hi, Consider i have a file, 'emp.txt' whose content is, EmpNo. Name Phone No. Salary 1 ABC 123 321 2 CBA 456 543 Now i want to change the phone no. 1st Employee alone. When i tried using ios:ate, all the contents of the file got deleted and the new phone no. got inserted. How can i solve this...

How to compare two pointers to object by their most derived type?

I have a following class hierarchy: class Base{ .... virtual bool equal(Base *); } class Derived1: public Base{ .... virtual bool equal(Base *); } class Derived2: public Derived1{ } class Derived3: public Derived1{ } class Derived4: public Base{ } How I should write Base::equal(Base *) function such that compares Derived4 and similar...

C++: Element types in a tuple

std::pair has the nested typedefs first_type and second_type which give the type of the first and second element respectively. But is there any way to statically determine the type of the Nth element in a boost::tuple (or std::tuple in C++0x)? I know I could create my own template with N as a parameter, and use it to recursively traver...

How to catch exception from CloseHandle()

As of the MSDN spec, CloseHandle throws an Exception if an invalid handle is passed to it when it runs under a debugger. Since I want to have clean code, I've inserted some code to catch it. However, it doesn't work, the exception gets uncaught. #include <windows.h> #include <tchar.h> #include <exception> /* omitted code */ CloseHandle...

How can I indent cout output?

I'm trying to print binary tree void print_tree(Node * root,int level ) { if (root!=NULL) { cout<< root->value << endl; } //... } How can I indent output in order to indent each value with level '-' chars. ...

Visual C++ studio, recompiling only the modified files

I have two dll files made by around 1500 cpp files. When I need to edit one, I usually then recompile all the 1500 files from the start. But I heard there is a way to make Visual Studio recompile the modifies only, taking a lot less time... How do I do this? ...

C++: Polymorphic class template

Consider a class Calendar that stores a bunch of Date objects. The calendar is designed to hold a collection of any type of objects that inherit from Date. I thought the best way to do it is to have a class template such as template<typename D> class Calendar{ ... } But it struck me that D can now in fact be any class. My questio...

ios::ate not working properly in Dev-Cpp

ios::ate is not working for changing only a part of file. is there any other way to achieve the same. I m using Dev-cpp its not working there. it clears all content and writing new. so help me out in this. ...

Prepending a string to another string

Currently, I have this code to prepend a "tag" to an exception message which gives me a very light version of a stack trace: try { doSomething(); } catch (std::exception& e) { int size = 8 + _tcslen(e.what()); TCHAR* error = new TCHAR[size]; _sntprintf(error, size, TEXT("myTag: %s"), e.what()); std::exception x = std...

template which enforces interface

Is it possible to create a template accepting types which implement certain interface? For example, I want to say to template user: you can store anything in my container as long as it implements Init() and Destroy() methods. Thanks ...

Are c styled strings safe?

In c/c++ some people use c-styled strings like: char *str = "This is a c-styled string"; My question is is this safe? The way I see it is they created a char pointer that points to the first letter of a const array of chars, but can't some other thing eg another variable overwrite a portion of the char array in the memory? Thus causin...

python struct.pack equivalent in c++

I want a fixed length string from a number just like struct.pack present in python but in c++. I thought of itoa (i,buffer,2) but problem can be that its length will depend on platform. Is there any way to make it independent of platform ? ...

How to check for division by 7 for big number in C++?

I have to check, if given number is divisible by 7, which is usualy done just by doing something like n % 7 == 0, but the problem is, that given number can have up to 100000000, which doesn't fit even in long long. Another constrain is, that I have only few kilobytes of memory available, so I can't use an array. I'm expecting the numbe...

How to use string index in c++ arrays(like php)?

How to use string index in c++ arrays(like php)? ...

Stuck building a game engine

I'm trying to build a (simple) game engine using c++, SDL and OpenGL but I can't seem to figure out the next step. This is what I have so far... An engine object which controls the main game loop A scene renderer which will render the scene A stack of game states that can be pushed and popped Each state has a collection of actors and...

C++ and Java performance

Hi, this question is just speculative. I have the following implementation in C++: using namespace std; void testvector(int x) { vector<string> v; char aux[20]; int a = x * 2000; int z = a + 2000; string s("X-"); for (int i = a; i < z; i++) { sprintf(aux, "%d", i); v.push_back(s + aux); } } int main() { for ...

[C++] CreateProcessWithLoginW - Redirecting STDOUT

What I would like is to have the process start but have the input and output all be in the same console. if(CreateProcessWithLogonW(user,domain, pass, LOGON_WITH_PROFILE, NULL, cmd, 0, 0, 0, &sa, &pe)) { printf("[~] Process spawned with PID %X\n", pe.dwProcessId); } else { printf("[!] Failed to create process. Error Code: %X\n", GetL...

Standard practice for implementing threads in C++?

Hello SO World, I'm looking forward to an interview in C++ in the coming weeks. (yay) So I have been relearning C++ and studying up. Unfortunately I have realized that I've never implemented threads in C++, and am somewhat concerned about a quiz on concurrency. As far as I can tell, C++ uses pthreads in Linux and some other device in W...

How to use string and string pointers in C++

I am very confused about when to use string (char) and when to use string pointers (char pointers) in C++. Here are two questions I'm having. which one of the following two is correct? string subString; subString = anotherString.sub(9); string *subString; subString = &anotherString.sub(9); which one of the following two is correct?...