c++

C++: When is it acceptable to have code in the header file?

I've been taught to keep class definitions and code separate. However, I've seen situations where people would often include some bits of code in the header, e.g. simple access methods which returns a reference of a variable. Where do you draw the line? ...

Pointers assignment

What is the meaning of *(int *)0 = 0; It does compile successfully ...

How does using arrays in C++ result in security problems

I was told that the optimal way to program in C++ is to use STL and string rather than arrays and character arrays. i.e., vector<int> myInt; rather than int myInt[20] However, I don't understand the rational behind why it would result in security problems. ...

Will adding data members(At end) in Exportable struct cause problems ?

Exportable function has the struct as a one of the parameter. This dll is used by many Exes One of the Exe needs to send some additional data , so we have added one member at the end of the struct and distributed the dll. Now my question is, if we put the new dll in other Exes which is not aware of the Extra member cause problems ? ...

How can I change the value in a pair in maps

I can do: map<char*, int> counter; ++counter["apple"]; But when I do: --counter["apple"] // when counter["apple"] ==2; I got debugger hung up in VS 2008. Any hints? ...

How to best convert VARIANT_BOOL to C++ bool?

When using COM boolean values are to be passed as VARIANT_BOOL which is declared in wtypes.h as short. There are also predefined values for true and false: #define VARIANT_TRUE ((VARIANT_BOOL)-1) #define VARIANT_FALSE ((VARIANT_BOOL)0) Which is the best way to convert from VARIANT_BOOL to C++ bool type? Obvious variants are: compare...

Create SQL queries for SQLite with MFC primitives

I'm using SQLite to store my program's state. sqlite3_exec() accepts the SQL query as a string. So I have a lot of code that builds such queries by concatenating numerous CString instances and a feeling that I'm doing something wrong. Is there a better way of doing this staying within primitives provided within SQLite and MFC? ...

Integrating external applications with my applications

I have 2 desktop applications that I wish to integrate with external applications. One of the applications is extended with plugins which are developed by me, to provide specific features which are not common for all distributions. The situation can be described in the following diagram: As I mentioned, I want to integrate (receive an...

C++ template specialization question

Hello, I'm trying to implement a generic toString() functino that would work on all types. All our internal classes derive from Abstract which includes a signature for toString(). In other words, all our internal classes have in some form, a toString method. The problem is, the primitive types (int, char, double..) don't have a native...

Setting file attributes by using C/C++ native functions

Hi, I know how to set file attributes on Windows by using SetFileAttributes function. But I am wondering if there is a native C/C++ function can can do this too? Thanks in advance. ...

When to use -O2 flag for gcc?

If I use "-O2" flag, the performance improves, but the compilation time gets longer. How can I decide, whether to use it or not? Maybe O2 makes the most difference in some certain types of code (e.g. math calculations?), and I should use it only for those parts of the project? EDIT: I want to emphasize the fact that setting -O2 for ...

convert pointer to shared_ptr

I have some library code (I cannot not change the source code) that returns a pointer to an object (B). I would like to store this pointer as shared_ptr under a class with this type of constructor: class A { public: A(boost::shared_ptr<B> val); ... private: boost::shared_ptr<B> _val; ... }; int main() { B *b = SomeL...

Write a circular file in c++

Hi, I need to write a circular file in c++. The program has to write lines in a file and when the code reaches a maximum number of lines, it must overwrite the lines in the beginning of the file. Anyone have any idea? Thanks ...

"Correct" way to send a sequence of UDP datagrams?

I was under the impression that the instability of UDP is a property of the physical layer, but seems that it isn't: I am trying to send a message over UDP, which is divided into a sequence of packets. Message identification and re-ordering is done implicitly. I tested this method over two apps that run on the same computer, and expect...

Is there a handy way of finding largest element in container using STL?

Dears, Is there a way finding largest container inside a container using STL? ATM, I have this rather naïve way of doing it: int main() { std::vector<std::vector<int> > v; ... unsigned int h = 0; for (std::vector<std::vector<int> >::iterator i = v.begin(); i != v.end(); ++i) { if (*i...

Problem using AddIPAddress when impersonating an Admin User

I am attempting to add a temporary IP address to a NIC using AddIPAddress when logged in as a non-admin user. The MSDN documentation for AddIPAddress states that ERROR_INVALID_HANDLE is returned as as error if the function is called by a non-admin user. Given that I have preceeded the call to AddIPAddress with API calls to LogonUser() a...

How to add files to Eclipse CDT project with CMake?

Hi, I'm having problem getting the source and header files added into my Eclipse CDT project with CMake. In my test project (which generates and builds fine) I have the following CMakeLists.txt: cmake_minimum_required(VERSION 2.6) project(WINCA) file(GLOB WINCA_SRC_BASE "${WINCA_SOURCE_DIR}/src/*.cpp") file(GLOB WINCA_SRC_HPP_BASE "${...

Overriding a Base's Overloaded Function in C++

I ran into a problem where after my class overrode a function of its base class, all of the overloaded versions of the functions were then hidden. Is this by design or am I just doing something wrong? Ex. class foo { public: foo(void); ~foo(void); virtual void a(int); virtual void a(double); }; class bar : public fo...

Enumerate members of a structure?

Is there a way to enumerate the members of a structure (struct | class) in C++ or C? I need to get the member name, type, and value. I've used the following sample code before on a small project where the variables were globally scoped. The problem I have now is that a set of values need to be copied from the GUI to an object, file, a...

Is it possible to use a custom class in place of std::pair in an STL map?

Is this possible? #include <map> class Example { private: std::map<std::string, std::string, less<std::string>, std::allocator< CustomPair<std::string, std::string> > > myMap; }; In the example above, CustomPair would be a template class holding a key and value. If this is possible, is it that simple or is there anything I s...