c++

C++ Storing objects in a file

I have a list of objects that I would like to store in a file as small as possible for later retrieval. I have been carefully reading this tutorial, and am beginning (I think) to understand, but have several questions. Here is the snippet I am working with: static bool writeHistory(string fileName) { fstream historyFile; historyFi...

Are the new and delete operators thread-safe in pthreads-w32 for visual c++?

Are the new and delete operators thread-safe in pthreads-w32 for visual c++? What things should I assume will always be thread-safe in pthreads-w32? ...

C++ Remove new line from multiline string

Whats the most efficient way of removing a 'newline' from a std::string? ...

Vector of ifstreams

How can I create and manipulate a vector of ifstreams? Something like this, except this doesn't work: vector<ifstream> Files(10, ifstream()); Files[0].open("File"); ...

How to solve ambiguous function overload due to default constructor in parameter

Let's say I have this class: class Foo { public: void member(std::string s); void member(int64_t &n); }; Now I want to do some thing like int64_t value = 5; Foo f; f.member(value); The problem is that the compiler (at least GCC) gets confused & believes I'm trying to call member with a string using the char* constructor: in...

Do threads clean-up after themselves in Win32/MFC and POSIX?

I am working on a multithreaded program using C++ and Boost. I am using a helper thread to eagerly initialize a resource asynchronously. If I detach the thread and all references to the thread go out of scope, have I leaked any resources? Or does the thread clean-up after itself (i.e. it's stack and any other system resources needed for ...

Center QGraphicsView in Widget

Hi, I have a QDialog that contains several dock widgets and one QGraphicsView. The widget layout is set to grid, the QGraphicsView size policy is set to fixed on the 2 axes and it the QGraphicsView is center in the empty zone of the QDialog. I would like to resize my QGraphicsView and let it at the center of the empty zone of the QDial...

architecture/design advise for a test program

I am trying to build a test program in c++ to automate testing for a specific application. The testing will involve sending requests which have a field 'CommandType' and some other fields to a server The commandType can be 'NEW', 'CHANGE' or 'DELETE' The tests can be Send a bunch of random requests with no pattern Send 100 'NEW' req...

C++ std::transform() and toupper() ..why does this fail?

Hi, I have 2 std::string. I just want to, given the input string: capitalize every letter assign the capitalized letter to the output string. How come this works: std::string s="hello"; std::string out; std::transform(s.begin(), s.end(), std::back_inserter(out), std::toupper); but this doesn't (results in a program crash)?...

From Static Typing to Dynamic Typing

I have always worked on staticly typed languages (c/c++,java). For the poast months i have been playing with clojure and i really like it. One thing i am worried about is, say that i have a windows that takes 3 modules as arguments and along the way requirements change and i need to pass another module to the function. I just change the ...

Template class + virtual function = must implement?

This code: template <typename T> struct A { T t; void DoSomething() { t.SomeFunction(); } }; struct B { }; A<B> a; is easily compiled without any complaints, as long as I never call a.DoSomething(). However, if I define DoSomething as a virtual function, I will get a compile error saying that B doesn'...

Efficient way to determine number of digits in an integer

What is a very efficient way of determining how many digits there are in an integer in C++? ...

C++ cin Question...

Okay, I was writing a simple C++ function to combine cin'd strings. I'm working on Linux at the moment, so I don't have the luxury of a simple "getline(cin, input)" command. Here's the code so far: string getLine() { string dummy; string retvalue; do { cin << dummy; retvalue ...

C/C++: Force Bit Field Order and Alignment

I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { unsigned int version : 3; unsigned int type : 1; unsigned int id : 5; unsigned int ...

How to efficiently copy istringstream?

Or ostringstream? istringstream a("asd"); istringstream b = a; // This does not work. I guess memcpy won't work either. ...

C++: Copy contructor: Use Getters or access member vars directly?

Have a simple container class: public Container { public: Container() {} Container(const Container& cont) //option 1 { SetMyString(cont.GetMyString()); } //OR Container(const Container& cont) //option 2 { m_str1 = cont.m_str1; } public string GetMyString() { return m_str...

Simple C Program

Ok so I am trying to learn C and I want my user to input a value so I am using scanf. I started off not having the flushes, because nothing was comming up until I typed in two values. Now that I have them though I get the same problem there still is no output until after I type in two numbers. Here is my code: #include <stdio.h> usin...

"Pinnacle" of Encapsulation - Question Regarding Advice from Effective C++

Item 23 of Effective C++ states: Prefer non-member non-friend functions to member functions. The whole purpose of the item was to encourage encapsulation, as well as package flexibility and functional extensibility, but my question is how far do you go when it comes to taking this advice? For example, you could have your class, your p...

Best Type for UTF-8 data?

What is the best type, in C++, for storing UTF-8 string? I'd like to avoid rolling my own class if possible. My original thought was std::string -- however, this uses char as the underlying type. char may be unsigned or signed - it varies. On my system, it's signed. UTF-8 code units, however, are unsigned octets. This seems to indicate ...

Mixing RTTI flags in C++

If I have multiple linked C++ statically linked libraries in C++, is it possible for them to share (pass to and from functions) class objects if they have been compiled with differing values of enabled/disabled run time type information (RTTI)? --edit: Thanks for the responses, the specific things I was worried about was 1. Does enablin...