Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Search the Tips
Tip Bank > C++ > Operators

Operators - Page 2

21-40 of 60     Previous     Next
Effects of Operator delete
by DevX Pro
Why, after deleting an object I allocated dynamically, is the pointer still there? What exactly have I deleted and what would happen if I assigned a new value to the pointer without nullifying it first?
Sizeof Operator
by DevX Pro
When declaring an unbounded array in my function's parameter list, int a[], can I use the sizeof() operator to determine the array's length at runtime?
Explaining the Modulus Operator
by DevX Pro
What does the modulus (%) operator do?
General Precedence
by DevX Pro
I am an engineer at GNB Technologies and have been programming for quite some time. Last week a friend of mine asked me an interesting question that stumped me. It's very simple. The code example is as follows: #include int main () { int x, num = 5; num = ++num + num++; cout The output is 13 and 12, even though the equation for variables num and x are the same. I have studied my precedence rules and the only explanation I have is that the compiler may be mistaking the + operator for a unary one, which would explain the result. Could you help me resolve the reason for this discrepancy?
Unconsting a Const Reference or Object
by DevX Pro
I have encountered a problem as follows: void foo(const int& var){ foo2(var); } void foo2(int& var) ... So what I did was void foo(const int& var){ foo2((int&)var); ^^^^^^ } and it compiled *_* and worked fine. However, is it OK to cast away const & to & and pass in? What is actually happening when I cast the variable—if the var is an object, is the constructor called?
Overloading Operator
by DevX Pro
How do I overload the ++ operator to work with an enum variable? For example, I defined SuitType to be {Heat, Club, Diamond, Spade}. I want to use s++ (where s is a variable of SuitType) so that it will advance s from Hearts, to Clubs, to Diamonds, etc.
Deep Copy and Shallow Copy
by Danny Kalev
The terms "deep copy" and "shallow copy" refer to the way objects are copied, for example, during the invocation of a copy constructor or assignment operator. In a deep copy (also called "memberwise ...
Overload Operator []
by DevX Pro
Is it possible to overload the 'subscripting operator' [] so it will take char * as the index, similar to an array? For example: I want to write a class which will store 'name' and 'age'. class_profile X X["Mary"] = 18; X["Mike"] = 20; X["John"] = 12; So it will work in such condition. if ( X["John"]
Overloading The ! Operator
by DevX Pro
Is it possible to have a postfix version of the ! operator?
Using the ?: operator as opposed to if...else...
by DevX Pro
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, that in my opinion is just as readable, as in the following example? iNum3 = (iNum1 > iNum2) ? iNum1 : iNum2; In this instance, I would prefer to use the ?: operator. Is there a problem in doing this? I have heard the argument that it is difficult to spot, and that it can be mininterpreted. Also, because it produces identical executable code, it may be better to use just the if...else... construct. However, I disagree with these comments as the person who tried to get me to accept them was looking through code that uses the ?: operator and spotted the use of them instantly and knew exactly what they did. What do you think?
Guidelines for Overloading the + Operator
by Danny Kalev
The built-in + operator is a binary operator that takes two arguments of the same type and returns the sum of its arguments without changing their values. In addition, + is a commutative operator. ...
Ensure Your Overloaded Subscript Operator Returns an Object by Reference
by Danny Kalev
When you overload the subscript operator, remember that the non-const version of the overloaded operator should return an object by reference, not by value. Otherwise, you won't be able to use it in ...
Alternative Representations of C++ Operators
by Danny Kalev
C++ defines textual representations for logical operators. Platforms and hardware equipment that do not support all the special characters used as logical operators can use these alternative ...
Overloading the Function Call Operator
by Danny Kalev
Overloading the function call operator can be somewhat confusing because the overloaded operator has two pairs of parentheses. It may not be immediately obvious which of these pairs declares the ...
Overloading the Subscript Operator the Right Way
by Danny Kalev
It is customary to overload the subscript operator, [], in classes that hold a sequence of elements. Vector and String are examples of such classes. When you overload operator [], remember to ...
Operators Can Only be Overloaded for User-Defined Types
by Danny Kalev
An overloaded operator must take at least one argument of a user-defined type (operators new and delete are an exception). This rule ensures that users cannot alter the meaning of expressions that ...
Overload New and Delete in a Class
by Danny Kalev
It is possible to override the global operators new and delete for a given class. For example, you can use this technique to override the default behavior of operator new in case of a failure. ...
Overloading Operators for enum Types
by Danny Kalev
For some enum types, it may be useful to define overloaded operators, such as ++ and --, that can iterate through the enumerator ...
Member and Non-Member Overloaded Operators
by Danny Kalev
Most of the overloaded operators can be declared either as non-static class members or as non-member functions. In this example, operator == is overloaded as a non-static class ...
Overloaded Operators May Not Have Default Parameters
by Danny Kalev
Unlike ordinary functions, overloaded operators cannot declare a parameter with a default value (overloaded operator() is the only ...
21-40 of 60     Previous     Next