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...
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...
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...
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...
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...
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...
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 ...
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?
...
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...
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 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";
...
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-...
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 ??
...
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.
...
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...
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...
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...
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);
}
...
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...
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?
...