October 25, 1999

Exception in Destructor

Question: If an exception is thrown in a destructor will the data objects in the class be destructed properly? Answer: Throwing an exception from a destructor is a bad idea and you should avoid it. The problem is that the destructor may be invoked due to another exception (as part

string.h

Question: I am a beginner with C++ programming. I was doing fine until I came to the string data type. Here is my problem: #include #include This gives me errors. I know it does not have anything to do with iostream, so what could be wrong with the other header?

Strong Types

Question: How do you make the compiler generate an error when a variable is set to a value outide the range of the constrained type? For example; strong x; //strong is a type that //must have values range //between 1 thru 10 x=11 //I want a compiler error //here! Out

Using the ?: operator as opposed to if…else…

Question: Is there a problem with using the ?: operator instead of the if…else… construct? For example, see the following: if(iNum1 > iNum2) iNum3 = iNum1;else iNum3 = iNum2; The above requires 4 lines of code. Yes, it’s readable, but it could be written with just one line of code,

Introspection of classes

Question: In Java using the java.lang.reflect classes, one can find out the name, class, and methods of an object simply by having a reference to that object. Is there a similar way of doing this anonymous introspection of classes using C++, or is the only way to have subclasses implement

Calculating interest without floating types

Question: How can you caluculate interest using only integers and no double or floats? Answer: This question can be of interest for anyone who is confined to ineteger arithmetic. Many embedded systems and specific processors do not support floating data types. On some processors, which happen to support floating data

Passing a unction as an argument

Question: How do I pass a function as a parameter? Answer: First, let’s distinguish between a parameter and an argument. The parameter is the type of the object passed. An argument is the actual object passed. The following function takes a parameter of type ‘pointer to function that doesn’t return

C++ objects

Question: How can I declare a constant object that is actually constant? Answer: Here’s an example: const Date d; //create a const Date object Note that you can only invoke the const member functions of a const object.

anonymous unions

Question: I’m trying to find information regarding anonymous unions in C++. I hear that in C++, one is able to declare a union such as: union example{ int a; int b;} ; Afterward, one would be able to refer to the variables a and b without using member notation. I’ve

No more posts to show