c++

C++ initializing the dynamic array elements

const size_t size = 5; int *i = new int[size](); for (int* k = i; k != i + size; ++k) { cout << *k << endl; } Even though I have value initialized the dynamic array ele...

pimpl idiom and template class friend

I'm trying to use the pimpl idiom to hide some grungy template code, but I can't give derived classes of the body class friend access to the handle class. I get an error C2248 from MSVC 9 sp1. Here's some code to duplicate the error: // // interface.hpp // namespace internal{ template<class T> class specific_body; } class int...

Converting a C-string to a std::vector<byte> in an efficient way

I want to convert a C-style string into a byte-vector. A working solution would be converting each character manually and pushing it on the vector. However, I'm not satisfied with this solution and want to find a more elegant way. One of my attempts was the following: std::vector<byte> myVector; &myVector[0] = (byte)"MyString"; which...

Create from scratch, or build up on Scratch?

I'm considering building a visual programming language, akin to Scratch, for use by children (a.k.a. poor typists) in programming micro-controllers or robots. There is, for example, a project to build a graphical programming environment for the Arduino. I really like Scratch, and would like the graphical coding to be similar. Scratc...

Impossible to inherit from this object?

Following up on this question, people have suggested I go with "option 3" which might look like this: class b2Body { private: b2Body() {} friend class b2World; }; class Body : public b2Body { private: Body() {} friend class World; }; class b2World { public: b2Body *CreateBody() { return new b2Body; } }; class World : public b...

Is this an example of a memory leak?

int main(){ int * a = new int[10]; int * b = new int[10]; for(int i = 0 ; i < 10; i++) { a[i] = 1; b[i] = 1; } for(int i = 0 ; i < 10; i++) { a[i] = b[i]; //each a[i] is allocated 4 bytes on heap //when we copy b[i] into a[i] do we loose //the reference...

Can c++ create array of pointers to different classes dynamically loaded from dll?

Can c++ create array of pointers to different classes dynamically loaded from dll? ps.without headers with class definations ...

[C++] Optional parameter for reference to pointer?

Hi, how do I the second parameter become optional? template <typename T> inline void Delete (T *&MemoryToFree, T *&MemoryToFree2 = ){ delete MemoryToFree; MemoryToFree = NULL; delete MemoryToFree2; MemoryToFree2 = NULL; } I tried several things after the = operator, like NULL, (T*)NULL etc can this be done? the only way the c...

Variable number of parameters in function in C++

How I can have variable number of parameters in my function in C++. Analog in C#: public void Foo(params int[] a) { for (int i = 0; i < a.Length; i++) Console.WriteLine(a[i]); } public void UseFoo() { Foo(); Foo(1); Foo(1, 2); } Analog in Java: public void Foo(int... a) { for (int i = 0; i < a.length; i+...

Are array of pointers to different types possible in c++?

Are array of pointers to different types possible in c++? with example please) ...

MATLAB engine versus libraries created by MATLAB Compiler?

To call MATLAB code in C or C++, how do you choose between using the MATLAB engine and using the MATLAB Compiler mcc to create C or C++ shared libraries from your MATLAB code? What are their pros and cons? For the second method, see http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f2-9676.html Are there other ways to call ...

Benefits of Parallel programming

I have three similar questions: Which known applications have benefits of multicore processors? Which known applications use posix threads (pthreads)? What can pthreads do that Java threads cannot? ...

C++ functor - unexpected behaviour?

I have written this program, which sorts some ints using a functor: #include<iostream> #include<list> #include<set> using namespace std; struct IntSorter { unsigned int comparisons; IntSorter() { std::cout << "new intsorter" << std::endl; comparisons = 0; } bool operator() (const int &a, const int ...

l-value substr method in C++

I want to create a substr method in C++ in a string class that I made. The string class is based on C-style string of course, and I take care of the memory management. I want to write a substr(start, length) function that can work on the regular way: CustomString mystring = "Hello"; cout << mystring.substr(0,2); // will print "He" ...

High performance comparison of signed int arrays (using Intel IPP library)

We're trying to compare two equally sized native arrays of signed int values using inequality operations, <, <=, > and >=, in a high performance way. As many values are compared, the true/false results would be sotred in a char array of the same size of the input, where 0x00 means false and 0xff means true. To accomplish this, we're usi...

std::pow gives a wrong approximation for fractional exponents

Here is what I mean trying to do double x=1.1402 double pow=1/3; std::pow(x,pow) -1; result is 0 but I expect 0.4465 the equation is (1 + x) ^3= 1.1402, find x. ...

How to mix C++ and external buttons on seperate window?

I want to make a C++ button on Start>Run i.e but when I do it will not do signalled event? Im sorry I have seen that you do not get the question. Ok basically when you create a button with CreateWindowEx(); I want to do that but put on a different window with SetPArent which ive already done now the button does now work so I need my prog...

http client blocks on recv()

Hi, I need some help writing an http client. The trouble comes when I try to receive data from a webserver. The recv() call blocks the program. Any better direction would be extremely helpful, I'll post my code below: if ( argc != 2 ) { cerr << "Usage: " << argv[0]; cerr << " <URI>" << endl; return 1; } else { uri_string = argv[1...

Question about pointers and objects?

Just wondering, if I statically create an object that has a pointer as a data member and then the object goes out of scope, what happens to the pointer? Chuma ...

Example code of libssh2 being used for port forwarding

I'm looking for an example of how to use libssh2 to setup ssh port forwarding. I've looked at the API, but there is very little in the way of documentation in the area of port forwarding. For instance, using PuTTY plink, There is the remote port to listen on, but also the local port that traffic should be sent to. Is it the developers...