473,387 Members | 1,420 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,387 software developers and data experts.

assignment operator=

Dear All!
Why there is no operator= for any type? I trying:

typedef float mtx[4][4];
operator=(mtx a, mtx b)
{
}

compiler compaints that
error: "operator=" must be a member function
or
error: nonmember operator requires a parameter with class or enum type
why? Is that for all operators? I trying to read "The C++ programming
language special edition" and found nothing related.

Jul 22 '05 #1
9 3357
"Gyro" <ap******@softhome.net> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Dear All!
Why there is no operator= for any type?
Your question is based upon false premises.
The assignment operator is always available
for all the built-in types (except arrays),
as well as many of the standard library types.
I trying:

typedef float mtx[4][4];
operator=(mtx a, mtx b)
{
}

compiler compaints that
error: "operator=" must be a member function
This is true in some cases.
or
error: nonmember operator requires a parameter with class or enum type
This is true.
why?
Because the language says so.
Is that for all operators?
Yes. Any operator you overload must be either a member
of a user defined type, or if it is not, at least one
of the arguments must be a user defined type.

Your 'typedef'd type above does not qualify, it
merely gives an alternate name for the array
(which is not a user defined type).

Them's the rulz. :-)

I trying to read "The C++ programming
language special edition" and found nothing related.


Look again.

Anyway, if you want to define your own operator=() which
does whatever to an array, you can wrap such an array
inside a class, and create an operator=() member function,
or write nonmember function which takes two arguments of
your 'wrapping' type.

class myArray
{
float mtx[4][4];
public:
myArray& operator=(const myArray& rhs)
{
/* code implementing assignment goes here */
}

/* etc */
};

Or
class myArray
{
float mtx[4][4];
/* etc */
};

myArray& operator=(myArray& lhs, const myArray& rhs)
{
/* code implementing assignment goes here */
}

But is there a reason you cannot use a std::vector<std::vector>
for your matrix?

If you're having difficulty with the Stroustrup book
(don't feel bad, many do), perhaps you might want to
try "Accelerated C++" by Koenig & Moo (www.acceleratedcpp.com)
and/or F. Glassborow's "You Can Do It" (I don't have link
handy, I'm sure google can find it).

HTH,
-Mike
Jul 22 '05 #2
Thank you for explanation.
The problem is I old c&pascal&basic&assembler programmer and relative
new in c++ (not in C). Stroustrup`s book is one of the bad books that I
seen ever (paradoxal, isn`t it?) - he is covers all aspects, but
without any explanation WHY this is invented or where it may be
practical. So his books like Linear algebra course - pure theoreticcal
and uninteresting for readers. Furtermore, i think his approach is
wrong. Starting with templates, namespaces, stl... Instead of normal
progressive narration with explanations why is this invented and where
this is usable....

Jul 22 '05 #3
> Why there is no operator= for any type?
there is.
I trying: typedef float mtx[4][4]; typedef does not define new types, it just defines an alias for some
other existent type, in that case to "float [4][4]". operator=(mtx a, mtx b)
{
}

compiler compaints that
error: "operator=" must be a member function
or
error: nonmember operator requires a parameter with class or enum type
why? Is that for all operators? I trying to read "The C++ programming
language special edition" and found nothing related.

to overload an operator you need to have at least one argument with a
user-defined type (class type). since "float [4][4]" is not a user
defined type, you can't overload operators with it.
if you want to overload, use something like this:
struct matrix4x4f
{
float elements[4][4];
};
or just use some existing matrix-class.
Bye,
'monster
Jul 22 '05 #4


Gyro ha scritto:
Thank you for explanation.
The problem is I old c&pascal&basic&assembler programmer and relative
new in c++ (not in C). Stroustrup`s book is one of the bad books that I
seen ever (paradoxal, isn`t it?) - he is covers all aspects, but
without any explanation WHY this is invented or where it may be
practical. So his books like Linear algebra course - pure theoreticcal
and uninteresting for readers. Furtermore, i think his approach is
wrong. Starting with templates, namespaces, stl... Instead of normal
progressive narration with explanations why is this invented and where
this is usable....


I think you speak pretty for yourself here. Moreover, Stroustrup book
isn't meant as a tutorial. If you want a book that teach you C++, try
with Eckel's "Thinking in C++" (freely downloadable at
http://www.mindview.net). Stroustrup's book is about C++, not about
teaching it, and actually it is damn well written.
Jul 22 '05 #5
"Gyro" <ap******@softhome.net> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Thank you for explanation.
The problem is I old c&pascal&basic&assembler programmer
I've also used all those languages, and many more.
They're all different, and need different 'mindsets'
when using them. Realizing that should help greatly
with learning a new language (e.g. when learning
BASIC, don't try to 'think' in assembly or Pascal).
and relative
new in c++ (not in C).
I also consider myself a 'newcomer' to C++. That's
why I read so many books about it.
Stroustrup`s book is one of the bad books that I
seen ever (paradoxal, isn`t it?)
I don't think it's a 'bad' book at all. I think it's
very good. Is it difficult to understand? For me, yes,
many parts of it are. But imo that's not a failing of the book,
but of me. My solution: read it again and again, practice, and
read *other books*. I find having something explained from more
than one perspective to be invaluable when learning something new.
- he is covers all aspects, but
without any explanation WHY this is invented or where it may be
practical.
Check out Stroustrup's "Design and Evolution of C++" (often
referred to as "D&E") for those kinds (e.g. 'why') of issues.
So his books like Linear algebra course - pure theoreticcal
and uninteresting for readers.
Why would you say theory is 'uninteresting for readers'?
For many/most (especially technical) disciplines, understanding
theory is critical for effectively applying them.
Furtermore, i think his approach is
wrong.
Perhaps it is, for *you*. Others find it a very good one.
Try different books. No book (however 'good' it might be)
is the best for everyone. See www.accu.org for peer
reviews and recommendations.
Starting with templates, namespaces, stl... Instead of normal
progressive narration with explanations why is this invented
As far as I can tell, TCPPL's goal is not to explain 'why',
but 'how'.
and where
this is usable....


The book is full of examples.

Anyway, with a subject as vast as C++, I would never try
to get all the knowledge about it from a single source,
even if it's from the inventor himself. I own no less
than two dozen C++ books. (Yes, I know not everyone has
the money for that. That's what libraries are for. :-))

-Mike
Jul 22 '05 #6
Mike Wahler wrote:
and relative
new in c++ (not in C).

I also consider myself a 'newcomer' to C++. That's
why I read so many books about it.


You've been using C++ for about 6 years AFAICT. At what point would you
stop being a newcomer?

Tom
Jul 22 '05 #7
"Tom Widmer" <to********@hotmail.com> wrote in message
news:41**********************@news.easynet.co.uk.. .
Mike Wahler wrote:
and relative
new in c++ (not in C).

I also consider myself a 'newcomer' to C++. That's
why I read so many books about it.


You've been using C++ for about 6 years AFAICT.


Closer to seven. :-)
At what point would you
stop being a newcomer?


Maybe about seven more (that is if I can manage to keep
up with new developments from ISO. :-))
Maybe 'newcomer' was the wrong word. Perhaps 'student'
of C++ would be more accurate.

-Mike
Jul 22 '05 #8
Mike Wahler wrote:

[ ... ]
At what point would you
stop being a newcomer?


Maybe about seven more (that is if I can manage to keep
up with new developments from ISO. :-))
Maybe 'newcomer' was the wrong word. Perhaps 'student'
of C++ would be more accurate.


I'm not sure anybody ever really stops being a student unless they just
quit using it entirely. I've used C++ quite regularly since sometime
around 1987 or so, and I still have a LOT left to learn!

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #9

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Mike Wahler wrote:

[ ... ]
At what point would you
stop being a newcomer?
Maybe about seven more (that is if I can manage to keep
up with new developments from ISO. :-))
Maybe 'newcomer' was the wrong word. Perhaps 'student'
of C++ would be more accurate.


I'm not sure anybody ever really stops being a student unless they just
quit using it entirely. I've used C++ quite regularly since sometime
around 1987


1997 for me. I consider myself fortunate in that, because
I almost immediately had a formal standard available. (This
was not the case when I first learned C, with the resulting
frustrations when moving among implementations and platforms)
or so, and I still have a LOT left to learn!


Exactly the point I was trying to make. ;-)

Paraphrasing a quote I saw somewhere:

"A good measure of intelligence is the acknowledgement
of how much one does *not* know."

-Mike
Jul 22 '05 #10

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
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...
1
by: Jon Slaughter | last post by:
I have a chain of classes(i.e., a series of classes each containing an array of the next class). Each class has array like access. struct Myclass1 { vector(Myclass2) _Myclass2; Myclass2&...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
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...
9
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.