Click here to Skip to main content
16,005,178 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: keyboard shortcuts for emoticons Pin
Hans Dietrich9-May-07 2:50
mentorHans Dietrich9-May-07 2:50 
QuestionHow to print a file which got images, table, text an so on Pin
KienNT789-May-07 1:11
KienNT789-May-07 1:11 
AnswerRe: How to print a file which got images, table, text an so on Pin
Hans Dietrich9-May-07 1:30
mentorHans Dietrich9-May-07 1:30 
AnswerRe: How to print a file which got images, table, text an so on Pin
Hamid_RT9-May-07 7:24
Hamid_RT9-May-07 7:24 
Questionoperator overloading Pin
Kiran Pinjala9-May-07 0:55
Kiran Pinjala9-May-07 0:55 
AnswerRe: operator overloading Pin
Hans Dietrich9-May-07 1:24
mentorHans Dietrich9-May-07 1:24 
GeneralRe: operator overloading Pin
Kiran Pinjala9-May-07 3:38
Kiran Pinjala9-May-07 3:38 
GeneralRe: operator overloading Pin
Hans Dietrich9-May-07 4:04
mentorHans Dietrich9-May-07 4:04 
Consider a class for Complex numbers, which has an addition operator defined as a member:
class Complex
{
public:
   Complex(double re, double im = 0);
   Complex operator+(const Complex& rhs);
private:
   double re, im;
};
We would like to be able to write code like this:
Complex c1(1,1);
Complex c2 = c1 + 4.5;
Complex c3 = 10 + c1;
The first statement simply constructs the complex number c1 = 1 + 1i. The complex number c2 is obtained by adding 4.5 to c1. Writing the assignment in functional form yields:
c2.operator= ( c1.operator+ ( 4.5 ) );
The addition operator takes a Complex argument. So how does this work when a real number, 4.5, is passed as the argument to operator+? It turns out that the compiler uses the constructor to implicitly convert 4.5 into a Complex by creating a temporary variable in order to perform the addition:
Complex c1 (1,1);
{
   Complex t1 (4.5);
   Complex c2 = c1 + t1;
}
The braces are used to show the scope of the temporary t1 which only exists for the duration of the addition. In functional form this can be written:
c2.operator= ( c1.operator+ ( t1 ) );
Next consider the assignment of c3. In functional form, the statement is written:
c3.operator= ((10.0).operator+ ( c1 ));
In order for this to work, 10.0 must be converted to a Complex. However, the compiler will not implicitly convert the left operand to call a member function on it.

If the addition operator is implemented as a non-member:
Complex operator+ (const Complex& a,
                   const Complex& b);
the compiler will implicitly convert both arguments as needed so that the assignment of c3 will compile. The operator may or may not be a friend depending on how it is implemented.

Implicit conversions are also used for conversion of initializers, function arguments, function return values, expression operands, expressions controlling iteration and selection statements, and explicit type conversions.

Implicit conversions (for left operands) will be performed implicitly by the compiler when the operator is a non-member. If type conversions are undesirable, the operator should be implemented as a member function. Most binary operators, such as addition, should be implemented as non-members since implicit type conversions are desired.

For more details, please consult C++ reference book.

Best wishes,
Hans
GeneralRe: operator overloading Pin
Kiran Pinjala9-May-07 4:06
Kiran Pinjala9-May-07 4:06 
QuestionDissasembler source code Pin
zon_cpp9-May-07 0:43
zon_cpp9-May-07 0:43 
AnswerRe: Dissasembler source code Pin
Hans Dietrich9-May-07 0:47
mentorHans Dietrich9-May-07 0:47 
GeneralRe: Dissasembler source code Pin
zon_cpp9-May-07 22:35
zon_cpp9-May-07 22:35 
QuestionHelp! Plot 3D real time. Pin
Aint9-May-07 0:17
Aint9-May-07 0:17 
AnswerRe: Help! Plot 3D real time. Pin
Hans Dietrich9-May-07 0:44
mentorHans Dietrich9-May-07 0:44 
QuestionHow to change size of DialogBox at run time? Pin
Atul238-May-07 23:47
Atul238-May-07 23:47 
AnswerRe: How to change size of DialogBox at run time? Pin
_AnsHUMAN_ 8-May-07 23:56
_AnsHUMAN_ 8-May-07 23:56 
GeneralRe: How to change size of DialogBox at run time? Pin
Atul239-May-07 2:01
Atul239-May-07 2:01 
AnswerRe: How to change size of DialogBox at run time? Pin
prasad_som9-May-07 2:15
prasad_som9-May-07 2:15 
AnswerRe: How to change size of DialogBox at run time? Pin
Nibu babu thomas9-May-07 0:48
Nibu babu thomas9-May-07 0:48 
AnswerRe: How to change size of DialogBox at run time? Pin
ThatsAlok16-May-07 20:36
ThatsAlok16-May-07 20:36 
Questionget the table name in a .mdb file Pin
hero19958-May-07 23:40
hero19958-May-07 23:40 
QuestionPrint a CString Pin
Epi8-May-07 23:16
Epi8-May-07 23:16 
AnswerRe: Print a CString Pin
Hans Dietrich9-May-07 0:39
mentorHans Dietrich9-May-07 0:39 
AnswerRe: Print a CString Pin
Hugo González Castro9-May-07 4:22
professionalHugo González Castro9-May-07 4:22 
GeneralRe: Print a CString Pin
Epi9-May-07 22:11
Epi9-May-07 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.