c++

Lifetime of a const string literal returned by a function

Consider this code: const char* someFun() { // ... some stuff return "Some text!!" } int main() { { // Block: A const char* retStr = someFun(); // use retStr } } My question is in the function sumFun() where is "some Text!!", stored (i think may be in some static area in ROM) and what will be its scope? Wi...

Possible to have C++ anonymous functions with boost?

I'm trying to solve a problem that anonymous functions make much, much easier, and was wondering if this was possible in c++. What I would like to do is (essentially) template<typename T> T DoSomething(T one, function<T(T)> dosomething) { return one + dosomething(5); } void GetMyVal(...) { DoSomething<int>(1, /*anonymous func h...

How to initialise a STL vector/list with a class without invoking the copy constructor

I have a C++ program that uses a std::list containing instances of a class. If I call e.g. myList.push_back(MyClass(variable)); it goes through the process of creating a temporary variable, and then immediately copies it to the vector, and afterwards deletes the temporary variable. This is not nearly as efficient as I want, and sucks whe...

Constructor return value

Could you tell me what is wrong with my class constructor? Code: CVector::CVector (int size_) { if (size_ > 0) { this->size = size_; this->data = new double[size]; for (int i = 0; i < size; i++) { (*this)(i) = i; } } cout << "constructor end" << endl; return; } Usage example: tvector = CVector(6); I get an access ...

pointer to preallocated memory as an input parameter and have the function fill it

Test code: void modify_it(char * mystuff) { //last element is null i presume for c style strings here. char test[7] = "123456"; //when i do this i thought i should be able to gain access to this //bit of memory when the function is destroyed but that does not //seem to be the case. //static char test[] = "123123"; ...

Does a c/c++ compiler optimize constant divisions by power-of-two value into shifts?

Question says it all. Does anyone know if the following... size_t div(size_t value) { const size_t x = 64; return value / x; } ...is optimized into? size_t div(size_t value) { return value >> 6; } Do compilers do this? (My interest lies in GCC). Are there situations where it does and others where it doesn't? I would re...

C++ Return by reference

Say, i have a function which returns a reference and i want to make sure that the caller only gets it as a reference and should not receive it as a copy. Is this possible in C++? In order to be more clear. I have a class like this. class A { private: std::vector<int> m_value; A(A& a){ m_value = a.m_value; } public: A() {}...

Intellisense fails for boost::shared_ptr with Boost 1.40.0 in Visual Studio 2008

I'm having trouble getting intellisense to auto-complete shared pointers for boost 1.40.0. (It works fine for Boost 1.33.1.) Here's a simple sample project file where auto-complete does not work: #include <boost/shared_ptr.hpp> struct foo { bool func() { return true; }; }; void bar() { boost::shared_ptr<foo> pfoo; pfoo.get()...

How are VST Plugins made?

I would like to make (or learn how to make) VST plugins. Is there a special SDK for this? how does one yield a .vst instead of a .exe? Also, if one is looking to make Audio Units for Logic Pro, how is that done? Thanks ...

Precision of cos(atan2(y,x)) versus using complex <double>, C++

Hi all, I'm writing some coordinate transformations (more specifically the Joukoswky Transform, Wikipedia Joukowsky Transform), and I'm interested in performance, but of course precision. I'm trying to do the coordinate transformations in two ways: 1) Calculating the real and complex parts in separate, using double precision, as below:...

Pass C++ object to Lua function

Hello there. I have a C++ project, where 1 method of a 1 class changes very often. So I want to take that code from C++ to Lua. Note, I'm novice to Lua. The whole task: Bind some class methods to Lua state machine; Pass reference to class object to a function, written in Lua; Operate with passed C++ object in Lua function. I've foun...

recognise @param in Eclipse for c++?

Hi All I am using Eclipse (3.5.1), on Ubuntu 9.10 to write some C++ code. I was searching through endless settings but didnt find what I as looking for... How can I force eclipse to make @param,(@see, @return etc) to be bold in the comments? All the documentation will be generated with the doxygen so I dont really need anything else....

How to check for C++ copy ellision

I ran across this article on copy ellision in C++ and I've seen comments about it in the boost library. This is appealing, as I prefer my functions to look like verylargereturntype DoSomething(...) rather than void DoSomething(..., verylargereturntype& retval) So, I have two questions about this Google has virtually no documen...

Is there an alternative to libusb-win32 for 64bit windows?

I've been developing some software which uses the libusb-win32 library to interact with some USB hardware I've been developing. Now I'm trying to run the same software on windows 64 but the drivers don't seem to work (understandably). Are there any alternatives for 64 bit Windows I've overlooked? ...

C++ Newbie: Passing an fstream to a function to read data

I have a text file named num.txt who's only contents is the line 123. Then I have the following: void alt_reader(ifstream &file, char* line){ file.read(line, 3); cout << "First Time: " << line << endl; } int main() { ifstream inFile; int num; inFile.open("num.txt"); alt_reader(inFile, (char*)&num); cout << "...

If statement not effective

void spriteput(int x,int y, int stype) { char sprite1[5]="OOOO"; char sprite2[5]="OOOO"; char sprite3[5]="OOOO"; char sprite4[5]="OOOO"; if (stype == 1) { char sprite1[5] = " OO "; char sprite2[5] = "OOOO"; char sprite3[5] = "OOOO"; char sprite4[5] = " OO "; mvprintw(2,y,"%s...

How do I do modulus in C++?

How do I perform a mod operation between two integers in C++? ...

Getting lpfnAcceptEx (accpetex) to block in native C++

Hello, I've been trying to get the lpfnAcceptEX function in Win32 to block on accept. If this is not possible I was wondering if there was a flag I could accpet or some other function that I could wait on. Right now the program is simply continually creating accepted sockets with no connections behind them. Perhaps I am misunderstanding...

using #define in a .pch

Hello All, If I define something in a pre-compiled header like: #define __BUILD_MAC__ and then in a header file do: #ifdef __BUILD_MAC__ typedef void* HINSTANCE; #endif This should work. But for some reason It isn't. I know because when I compile I get errors about HINSTANCE not naming a type. Do I need to do anything else...

How do I compile for windows XP under windows 7 / visual studio 2008

I'm running Windows 7 and Visual Studio 2008 Pro and trying to get my application to work on Windows XP SP3. It's a really minimal command line program so should have any ridiculous dependencies: // XPBuild.cpp : Defines the entry point for the console application. // #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { pr...