c++

Print Coloured Text to Console in C++

I would like to write a Console class that can output coloured text to the console. So I can do something like (basically a wrapper for printf): Console::Print( "This is a non-coloured message\n" ); Console::Warning( "This is a YELLOW warning message\n" ); Console::Error( "This is a RED error message\n" ); How would I print different...

constness of returned object(s) from a const member function

What is a good rule of thumb as to whether or not the return value of a const member function should be const or not? Here's what I tend to do, but I struggle with ambiguous scenarios. class foo { public: // if the returned object is conceptually // (and therefore usually literally but not even necessarily) ...

Set local environment variables in C++

How do I set an environment variable in C++? They do not need to persist past program execution They only need to be visible in the current process Preference for platform independent but for my problem only needs to work on Win32/64 Thanks ...

C++ Libraries: Questions need Definite Answers(Opinions)

I am fairly new to C++ programing, so I am not quite sure what I am looking for at the moment. I have experience with C#, Python (barely), and Visual Basic, but I am looking into using C++ and breaking away from .NET in general (before it completely sucks me in). My questions are as follows: 1) What would be the lightest weight, platfor...

C++ for 8051 microcontroller?

Hi all, Could someone please tell me if it's possible to burn an 8051 microcontroller with a C++ program? I've tried searching about it online but can't seem to find out for sure if it's possible or not. Keil uses C, but the program I need to write is very string-intensive and C is quite string-unfriendly as compared to C# which is wha...

A good way to do a fast divide in C++?

Sometimes I see and have used the following variation for a fast divide in C++ with floating point numbers. // orig loop double y = 44100.0; for(int i=0; i<10000; ++i) { double z = x / y; } // alternative double y = 44100; double y_div = 1.0 / y; for(int i=0; i<10000; ++i) { double z = x * y_div; } But someone hinted recently that t...

Why do people use enums in C++ as constants while they can use const?

Hello. I was just wondering why people use enums in C++ as constants while they can use const. Thanks ...

COM, COM+, DCOM, where to start?

I am curious about COM+, DCOM. I know that MSFT does not encourage you to use this tools natively (meaning with C/C++, in fact there is not a lot of documentation available) but I want to learn to use these technologies, like embedding Internet Explorer into a C program. I thought that maybe I could find people that worked with this or ...

When learning new languages related readings, Distractions or Aids?

When learning new languages such as C++ from PHP, does reading other language snippets help you understand better by giving one a different prospective, or does doing so confuse a noob like me? Also, any advice on learning C++ would be great. ...

Catching wrong array reference in C++

How do I catch wrong array reference in C++? Why doesn't the following code work: #include <exception> int * problemNum = new int; int (* p [100])() = {problem1, problem2, problem3}; ... try { cout << (*p[*problemNum-1])(); } catch (exception){ cout << "No such problem"; } My compiler...

difference in long vs int data types in C++

What's the difference between int and long in C++ since both: sizeof(int) ... and sizeof(long) return 4? ...

C++ project source code layout

One of the popular way to organize project directory is more or less like this: MyLib +--mylib_class_a.h mylib_class_a.cpp mylib_library_private_helpers.h mylib_library_private_helpers.cpp MyApp +--other_class.h other_class.cpp app.cpp app.cpp: #include "other_class.h" #include <myl...

Initializing pointers in C++

Can assign a pointer to a value on declaration? Something like this: int * p = &(1000) ...

import data from web stream in c++

hi i was wondering if is possible to import data from a live stream from a web site and perform computation on the data in real time? if this is possible what is the most efficient(computationally fast) way of doing it? thank you for any help or commpents. ...

Limit on cout stream?

A puzzle that hit me. In some simple test harness code, if I stream too many characters to stdout, the program fails. Strange but very reproducable. This may be a Windows only issue, but it's easy to see: #include <iostream> #include <deque> using namespace std; int main() { deque<char> d; char c; while (cin.get(c)) d.push_b...

how do I elegantly format string in C++ so that it is rounded to 6 decimal places and has extra '0's or '9's trimmed

How do I write a function that formats a string with decimals digits, without trailing 0's or unnecessary 9's? Given that decimals is 2, here's what I expect: 0.999 -> 1.0 0.99 -> 0.99 1.01 -> 1.01 1.001 -> 1.0 123 -> 123.0 0 -> 0.0 0.1 -> 0.1 (negatives as you'd expect) Here's what I have so far, but it's pretty ugly code. Is ther...

Why can't I use strerror?

I'm porting some code to Windows, and the Microsoft compiler (Visual C++ 8) is telling me that strerror() is unsafe. Putting aside the annoyance factor in all the safe string stuff from Microsoft, I can actually see that some of the deprecated functions are dangerous. But I can't understand what could be wrong with strerror(). It takes ...

When I created my helper classes, am I over designing?

I am a C++ programmer and recently joined a new company that uses a lot of C. When they reviewed my code, they were thinking I over-designed some of the things which I totally disagreed. The company is doing everything in embedded system, so my code needs to be memory efficient, but the stuff I am doing is not CPU intensive. I would like...

C++ boost shared_ptr as a hash_map key

Hi all, I'm making a neural network and wanted to use a hash_map to keep weight references for output neurons for each neuron: class Neuron; //forward declaration was there (sorry I forgot to show it earlier) typedef double WEIGHT; typedef stdext::hash_map<boost::shared_ptr<Neuron>,WEIGHT> NeuronWeightMap; class Neuron { private: N...

System() calls in C++ and their roles in programming

I've often heard that using system("PAUSE") is bad practice and to use std::cin.get() instead. Now my understanding of system calls is that they take a string which they enter into a system command line and talk with the OS, so PAUSE is a DOS command that pauses the output in the command window. I assume this works similarly with Mac and...