From: Keith Thompson on 2 Dec 2009 11:42 Pat <pkelecy@_REMOVETHIS_gmail.com> writes: > Keith Thompson wrote: >> Pat <pkelecy@_REMOVETHIS_gmail.com> writes: >>> Is it possible to define overloaded operators for structures? >> >> In which language? >> >> In standard C, no. In C++, yes; consult any C++ textbook. > > Yes, I'm working in C++. > > I have several C++ books, and the overloading examples they show all > involve class definitions. Do you know of any texts you could > recommend that have good examples of this being applied to structures? The *only* difference between a struct and a class, as far as the C++ language is concerned, is that struct members are public by default, and class members are private by default. There are other differences regarding when you *should* use "struct" vs. "class". Some might argue that, if you're defining overload operators for a type, you should make it a class rather than a struct, but it probably doesn't matter. See question 7.8 of the "C++ FAQ Lite", <http://www.parashift.com/c++-faq-lite/>. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Kaz Kylheku on 2 Dec 2009 11:50 On 2009-12-02, Pat <pkelecy@_REMOVETHIS_gmail.com> wrote: > Keith Thompson wrote: >> Pat <pkelecy@_REMOVETHIS_gmail.com> writes: >>> Is it possible to define overloaded operators for structures? >> >> In which language? >> >> In standard C, no. In C++, yes; consult any C++ textbook. >> > > Yes, I'm working in C++. > > I have several C++ books, and the overloading examples they show all > involve class definitions. Do you know of any texts you could recommend > that have good examples of this being applied to structures? structs and classes have exactly the same syntax, except for the different keyword. Access in a struct defaults to public, rather than private.
From: osmium on 2 Dec 2009 11:51 "Pat" wrote: news:B_OdnQPfuc6PCYvWnZ2dnUVZ_s-dnZ2d(a)insightbb.com... > Keith Thompson wrote: >> Pat <pkelecy@_REMOVETHIS_gmail.com> writes: >>> Is it possible to define overloaded operators for structures? >> >> In which language? >> >> In standard C, no. In C++, yes; consult any C++ textbook. >> > > Yes, I'm working in C++. > > I have several C++ books, and the overloading examples they show all > involve class definitions. Do you know of any texts you could recommend > that have good examples of this being applied to structures? You are unlikely to find such a book. It is a convention in C++ to use class for things with member functions, and struct for dumb, passive data carriers. Get your arms around what the word "convention" means. The difference in the language *definition* relates to public vs. private differences with respect to defaults. The advantage of the convention is that it is a documentation aid for humans.
From: Francis Glassborow on 2 Dec 2009 13:00 osmium wrote: > "Pat" wrote: > > news:B_OdnQPfuc6PCYvWnZ2dnUVZ_s-dnZ2d(a)insightbb.com... >> Keith Thompson wrote: >>> Pat <pkelecy@_REMOVETHIS_gmail.com> writes: >>>> Is it possible to define overloaded operators for structures? >>> In which language? >>> >>> In standard C, no. In C++, yes; consult any C++ textbook. >>> >> Yes, I'm working in C++. >> >> I have several C++ books, and the overloading examples they show all >> involve class definitions. Do you know of any texts you could recommend >> that have good examples of this being applied to structures? > > You are unlikely to find such a book. It is a convention in C++ to use > class for things with member functions, and struct for dumb, passive data > carriers. Get your arms around what the word "convention" means. The > difference in the language *definition* relates to public vs. private > differences with respect to defaults. The advantage of the convention is > that it is a documentation aid for humans. > > Well actually a convention in many places is the use of the struct keyword flags up public data. Sometimes it is right to have public data but it is nice to warn reader's of your code that it was intentional rather than through ignorance.
From: Pat on 3 Dec 2009 16:08
Thanks for all the replies on this. I spent part of the day playing around with this, and was able to define constructors, member functions, etc for a test "struct" following the examples given for a class construction. I was also able to define some overloaded operators as well, and it all seems to be working as expected. So I think I'm learning. I do have one other question though. My test structure looks like this: **************************************** struct Point { double x; double y; Point(double u=0, double v=0); //...Constructor Point operator*(const double & b) const; //...Allows multiplying a point by scalar. }; // Member functions Point::Point(double u, double v) //...Constructor { x=u; y=v; } Point Point::operator*(const double & b) const { return Point(x*b,y*b); } ***************************************** The overloaded operator works fine if the scalar comes after the point variable, but not if it comes before. ie. Point p1(1,2); //...Define point p1 Point p2 = p1*5; //...This works Point p3 = 5*pi //...This doesn't! Generates an error Is there anyway to define an overloaded * so 5*p1 works also? It's not a big deal, if not, but it would be convenient not to have to think about the order when it comes time to actually use this in my code. Thanks again for the help. -Pat |