473,378 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

operator + without =

Hi,

Can I define a class that has an operator+ set:

A operator+(A a, double d);
A operator+(double d, A a);
....

without defining an = operator for the class? Just different
constructors?

class A
{
A() {dat=new int[1]; *dat=0;}
A(double d) {dat=new int[1]; *dat=(int)d;}
};

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #1
2 1141

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2n***********@uni-berlin.de...
Hi,

Can I define a class that has an operator+ set:

A operator+(A a, double d);
A operator+(double d, A a);
...

without defining an = operator for the class? Just different
constructors?

class A
{
A() {dat=new int[1]; *dat=0;}
A(double d) {dat=new int[1]; *dat=(int)d;}
};


I don't see why not (at least from a language POV). Are you having any
specific difficulty?

john
Jul 22 '05 #2
Gernot Frisch wrote:
Can I define a class that has an operator+ set:

A operator+(A a, double d);
A operator+(double d, A a);
...

without defining an = operator for the class? Just different
constructors?

class A
{
A() {dat=new int[1]; *dat=0;}
A(double d) {dat=new int[1]; *dat=(int)d;}
};


"You have the right to define the operator=. If you give up your right,
the operator= will be defined for you" :-)

My comment here is: if you don't define the operator= _and_ your class
has some dynamic memory management in it, you are looking at a very
unpleasant result when you want to write

A pi(3.14159);
A e(2.7182818);
A d;

d = pi + e;

The 'dat' member will either be destroyed twice or you will have a memory
leak (depends on whether you have the d-tor defined correctly).

See "The Rule of Three".

Victor
Jul 22 '05 #3

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

Similar topics

9
by: Matthew Polder | last post by:
Hi, When a class Apple is written and the assignment operator is not explicitly declared, the operator Apple& operator=(const Apple&) is created by the compiler. Is there any difference...
10
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
9
by: Emanuele Aina | last post by:
I have some code which does a lot of "in" on lists containing objects with no __eq__ defined. It all goes fast until I add the __lt__() method: then I have a slowdown comparable to the one I get...
5
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...
2
by: aaragon | last post by:
Hi everyone, Can someone point me out why I can't declare the operator() of a functor as static? The reason behind this is that I want to be able to call to the function without instantiating...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
6
by: alessio211734 | last post by:
I have a class that defines a generic point where p={a1..an} template<class T,int iclass Point { public: T v; Point<T,i>() { for (int j=0;j<i;j++) v=0;
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.