473,700 Members | 2,459 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 2080
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
2072
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
3797
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
4671
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
2508
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
1575
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
2381
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
2292
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
3275
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
2972
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
1214
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
8726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8645
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
9214
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...
0
9075
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7810
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...
1
6561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.