Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About operator overload?

Reply
Thread Tools

About operator overload?

 
 
jay
Guest
Posts: n/a
 
      05-21-2006
In the c++ primer ,i get a program.
A class's name is TT,and it define the operator overload!

TT first; //constructor
TT second(30);//constructor
TT thrid(40://constructor
first=second.operator+;

the question is the fourth line is all right?

maybe that is :
first=second.operator+(third);

which one is right?

 
Reply With Quote
 
 
 
 
leconte
Guest
Posts: n/a
 
      05-21-2006
jay wrote:
> In the c++ primer ,i get a program.
> A class's name is TT,and it define the operator overload!
>
> TT first; //constructor
> TT second(30);//constructor
> TT thrid(40://constructor
> first=second.operator+;
>
> the question is the fourth line is all right?
>
> maybe that is :
> first=second.operator+(third);
>
> which one is right?
>

i think u r right
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-21-2006
jay wrote:
> In the c++ primer ,i get a program.
> A class's name is TT,and it define the operator overload!
>
> TT first; //constructor
> TT second(30);//constructor
> TT thrid(40://constructor
> first=second.operator+;
>
> the question is the fourth line is all right?


Even the third line is not alright. The fourth line is definitely missing
some parentheses.

> maybe that is :
> first=second.operator+(third);
>
> which one is right?


Depending on how the operator is defined, either could be right.

V
--
Please remove capital As from my address when replying by mail


 
Reply With Quote
 
asterisc
Guest
Posts: n/a
 
      05-21-2006
< TT first; //constructor
< TT second(30);//constructor
< TT thrid(40://constructor
< first=second.operator+;

< the question is the fourth line is all right?

< maybe that is :
< first=second.operator+(third);

< which one is right?

For example:
class TT
{
public:
TT() : Value( 0 ) {}
TT( int value ) : Value( value ) {}

TT operator+() // first overloading
{
return TT( this->Value );
}

TT operator+ ( const TT& op ) // second overloading
{
return TT( this->Value + op.Value );
}

protected:
int Value;
};

//----------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
TT first; //constructor
TT second(30); //constructor
TT third(40); //constructor

first = second.operator+(); // first overloading (it
maybe should be operator =)
first = second + third; // second overloading
std::cout << first.Value << std::endl;

return 0;
}

This is perfect legally code!
So, if at your 4-th statement, add some paranthesis, the code looks
fine

It's only depending on how your operator+ is defined!

 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      05-21-2006
asterisc <> wrote:

> int _tmain(int argc, _TCHAR* argv[])
> {
> TT first; //constructor
> TT second(30); //constructor
> TT third(40); //constructor
>
> first = second.operator+(); // first overloading (it
> maybe should be operator =)
> first = second + third; // second overloading
> std::cout << first.Value << std::endl;
>
> return 0;
> }
>
> This is perfect legally code!


It's not because _TCHAR is not defined anywhere. Also are you
missing a main function. _tmain is non-standard.

regards
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
asterisc
Guest
Posts: n/a
 
      05-21-2006
>> This is perfect legally code!
>
> It's not because _TCHAR is not defined anywhere. Also are you
>missing a main function. _tmain is non-standard.


Sorry, i use to work on windows platform, and i made a... quick
project.
It automaticaly includes:

#include <iostream>
#include <tchar.h>

But, ignore that, and replace: _tmain with main, and _TCHAR with char

Anyways, the main focus was the overloaded operators, and their
callings, not.. the main entry

 
Reply With Quote
 
Stuart Golodetz
Guest
Posts: n/a
 
      05-22-2006
"jay" <> wrote in message
news: ups.com...
> In the c++ primer ,i get a program.
> A class's name is TT,and it define the operator overload!
>
> TT first; //constructor
> TT second(30);//constructor
> TT thrid(40://constructor
> first=second.operator+;
>
> the question is the fourth line is all right?


You can find a way to make it work, if you correct the third line to TT
third(40); and write a program like the following:

#include <iostream>

class TT
{
private:
void (*m_p)(const TT&);
public:
TT()
: m_p(NULL)
{}

TT(int n)
: m_p(NULL)
{}

static void operator+(const TT& rhs) // dubious and contrived
{
std::cout << "Blah\n";
}

TT& operator=(void (*p)(const TT&))
{
m_p = p;
return *this;
}

void f()
{
(*m_p)(*this);
}
};

int main()
{
TT first;
TT second(30);
TT third(40);
first = second.operator+;
first.f();
return 0;
}

Is it a good idea? Almost invariably not (never say "never", of course!) But
it does go to show that it can be done if you really want to

HTH,
Stu

> maybe that is :
> first=second.operator+(third);
>
> which one is right?



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
T::operator int () const ambiguous with T::operator Handle () const? Tim Clacy C++ 15 05-30-2005 02:14 AM
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments