assignment operator=

G

Gyro

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.
 
M

Mike Wahler

Gyro said:
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.

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
 
G

Gyro

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....
 
M

Matteo Settenvini

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.
 
S

Swampmonster

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
 
M

Mike Wahler

Gyro said:
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
 
T

Tom Widmer

Mike said:
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
 
M

Mike Wahler

Tom Widmer said:
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
 
J

Jerry Coffin

Mike Wahler wrote:

[ ... ]
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!
 
M

Mike Wahler

Jerry Coffin said:
Mike Wahler wrote:

[ ... ]
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
 

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

No members online now.

Forum statistics

Threads
474,188
Messages
2,571,002
Members
47,591
Latest member
WoodrowBut

Latest Threads

Top