About operator overload?

J

jay

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?
 
L

leconte

jay said:
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:)
 
V

Victor Bazarov

jay said:
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
 
A

asterisc

< 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!
 
J

Jakob Bieling

asterisc said:
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
 
A

asterisc

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
 
S

Stuart Golodetz

jay said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top