473,761 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About operator overload?

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.op erator+;

the question is the fourth line is all right?

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

which one is right?

May 21 '06 #1
6 2084
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.op erator+;

the question is the fourth line is all right?

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

which one is right?

i think u r right:)
May 21 '06 #2
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.op erator+;

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.op erator+(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
May 21 '06 #3
< TT first; //constructor
< TT second(30);//constructor
< TT thrid(40://constructor
< first=second.op erator+;

< the question is the fourth line is all right?

< maybe that is :
< first=second.op erator+(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!

May 21 '06 #4
asterisc <ra*******@fian system.com> 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)
May 21 '06 #5
>> 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

May 21 '06 #6
"jay" <gu************ @gmail.com> wrote in message
news:11******** **************@ 38g2000cwa.goog legroups.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.op erator+;

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.op erator+(third);

which one is right?

May 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2078
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class type, but none of the new class type. However it does return a type of the new class. The compiler tells me that at least one parameter must be of the containing type. Is there a way to do this? The other class hos no reference to this new class. Is...
5
3805
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
7
4672
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++ copyconstructor1.cpp #copyconstructor1.cpp: In function `int main()': #copyconstructor1.cpp:86: no match for `sample& = sample' operator #copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&) ...
17
2510
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
7
1581
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will need use Overloaded + Operator ??? Thanks and sorry my bad English.
9
2382
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I attempt to check to see if the object is null: "The call is ambiguous between the following methods or properties: 'TestObject.operator ==(TestObject, string)' and 'TestObject.operator ==(TestObject, TestObject)" Does anyone have any idea how to...
5
2293
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
3
3280
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
8
2975
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
8
1217
by: Tony Johansson | last post by:
Hello! I think that this text is complete enough so you can give me an explanation what they mean with it. I wonder if somebody understand what this means. It says the following : "You can't overload assignment operator, such as +=, but these operator use their simple counterpart, such as +, so you don't have to worry about that. Overloading + means that += will function as expected. The = operator is included in this -- it makes little...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.