c++

reference to the pointed object

Dereferencing pointers can make the code very hard to read. What I usually do is putting a reference to the pointed object and working with the reference. Example: shared_ptr<std::vector<int> > sp = get_sp_to_vector(); std::vector<int>& vec = *sp; ... vec.push_back(5); I wonder if it's a good practice. Does it have any drawback? Upda...

Should I use "int" or "bool" as a return value in C++?

If I have a function that performs some procedure and then needs to return the truth value of something is there a compelling reason to use either a boolean variable, or an int variable, as the return type? bool Foo() { ... ... return truthValue; } int Foo() { ... ... return truthValue; } Is there an appreciable differe...

My version of C++ non-member getline(), that takes a FILE* (created by _wfopen()) instead of a stream, is too slow

In C++, you can use non-member getline() with a stream in a loop like this: #include <string> #include <fstream> #include <cstdlib> using namespace std; int main() { ifstream in("file.txt"); if (!in) { return EXIT_FAILURE; } for (string line; getline(in, line); ) { // Do stuff with each line } } Ho...

Segmentation fault when different input is given

hi all, I do some image processing work in C++. For this i use CImg.h library which i feel is good for my work. Here is small piece of code written by me which just reads an image and displays it. #include "../CImg.h" #include "iostream" using namespace std; using namespace cimg_library; int main(int argc,char**argv) { CImg<uns...

Why doesn't C++ implement construction + calling functions in same line?

I'm wondering why C++ (and possibly other languages, I'm not sure) doesn't allow statements like this. MyObject foo.DoBar(); You would think that the language could understand to construct the object, then call the function. The only reason I can think of this not working is that if the construction of the object failed, the statement...

A few sorting questions

I have found a way that improves (as far as I have tested) upon the quicksort algorithm beyond what has already been done. I am working on testing it and then I want to get the word out about it. However, I would appreciate some help with some things. So here are my questions. All of my code is in C++ by the way. One of the sorts I ha...

How to show Iplmage format on the GUI of C++ instead of showing it in another windows?

I'm trying to display the output image onto the C++ interface instead of creating another window to display the image. For example: cvNameWindow("Window A",0); cvShowImage("Window A", imgA); This will create an window and display the imgA on it. My question is that can i display my imgA onto my C++ interface together ...

Algorithm for allocating memory pages and page tables

I want to design an algorithm for allocating and freeing memory pages and page tables. What data structures would allow best performance and simplest implementation? ...

Defining custom hash function and equality function for unordered_map

I am trying to define a type of unordered_map that has a custom hash function and equality comparison function. The function prototypes of these functions are as follows: //set<Vertex3DXT*> is the type of the key; Cell3DXT* is the type of the value size_t VertexSetHashFunction(set<Vertex3DXT*> vertexSet); //hash function bool SetEqual(s...

Easy way find uninitialized member variables

I am looking for easy way to find uninitialized class member variable. Runtime or compile time both methods OK. Currently breakpoint in class constuctor and watch variable one by one. ...

Which one to use const char[] or const std::string?

Which is better for string literals, standard string or character array? I mean to say for constant strings, say const char name[] = "so"; //or to use const string name = "so"; ...

NPAPI plugin in QTWebKit

I know its possible to integrate NPAPI plugins with QTWebKit as its been supported since the release of QT 4.5. My question is, should I go and design my plugin according to the Mozilla/Gecko documentaion -Which is probably the only available/reliable documentation for NPAPI beside some really old book called "Programming NetScape Plug-...

porting from 32 bit to 64 bit

I have windows application build using Visual C++. Its being build and run on 32 bit windows env. Now I need to make sure it works on windows vista / 7 64 bit env. What all things I need to consider for this porting process ?? ...

Unsigned keyword in C++

Does the unsigned keyword default to a data type in C++? I am trying to write a function for a class for the prototype: unsigned Rotate(unsigned object, int count) But I don't really get what unsigned means. I thought it would be like unsigned int or something. Thanks. ...

checking for a null object in C++

Hi all, I've mostly only worked with C, and am running into some unfamiliar issues in C++. Let's say that I have some function like this in C, which would be very typical: int some_c_function(const char* var) { if (var == NULL) { /* exit early so we don't dereference a null pointer */ } /* rest of code */ } and l...

Object creation wrapper for a template class

Given a template class as such: template <typename TYPE> class SomeClass { public: typedef boost::intrusive_ptr<SomeClass<TYPE> > Client_t; inline Client_t GetClient() { return Client_t(this); } }; SomeClass is intended only to be used via pointer references returned by SomeClass::GetClient(). Which makes it natural to write a wr...

C++ STL container and in-place construction

Please consider the following: class CMyClass { public: CMyClass() { printf( "Constructor\n" ); } CMyClass( const CMyClass& ) { printf( "Copy constructor\n" ); } }; int main() { std::list<CMyClass> listMyClass; listMyClass.resize( 1 ); return 0; } It produces the following output: Constructor Copy...

C Macro definition to determine big endian or little endian machine ?

Is there a one line macro definition to determine the endianness of the machine. I am using the following code but converting it to macro would be too long. unsigned char test_endian( void ) { int test_var = 1; unsigned char test_endian* = (unsigned char*)&test_var; return (test_endian[0] == NULL); } ...

Multiple writes in boost::asio to a single socket

I couldn't find any thing about what happens if you try to do a second write to a boost::asio socket before a previous one completed. This seems like something that could potentially happen in many asynchronous programs (since after doing the first write, the program will then continue before waiting for it to finish, potentially causing...

Advantages/disadvantages of auto pointers

What are the advantages and disadvantages of using auto pointers (auto_ptr), compared to ordinary pointers? I've heard it does automatic releasing of memory but how come it is not used often? ...