Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-06-2004, 09:41 PM
|
#1
|
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,368
Rep:
|
c++ [] operator overloading
I am writing an array wrapper for personal use, I am not done with it, my question is on the [] operator overloading function defenitions, or more importantly how to do them, this si what I haev so far with the sections I nee dhelp in specified:
Code:
template<class T>
class exar
{
public:
exar();
exar(int In);
exar(T[] In);
int GetAG();
void SetAG(int h);
friend void operator ++();
// I need the help here:
friend void operator [](const int& in); // I need to be able to do nameofvariable[in] = value
friend T operator [](const int& in); // need to return the value of nameofvariable[in]
// end of what I need help with
friend istream& operator >>(istream& in, exar& out);
friend ostream& operator <<(ostream& out, const exar& in);
private:
int AutoGrow = 10; // Ammount to grow by when needed
T InAr[1][AutoGrow];
};
anyone know what to do in there?
added: I will not be using friend functions, I will change them to member functions.
I did some googling and eventually found this:
T& operator [](const int& in);
am I correct in thinking this works for both name[in] = value; and somevar = name[in]; ?
Last edited by exodist; 04-06-2004 at 09:56 PM.
|
|
|
|
04-06-2004, 09:56 PM
|
#2
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
I think your over thinking this,
Code:
T &operator[]( const int i ){ return InAr[i]; }
const T &operator[]( const int i ){ return InAr[i]; }
this isn't exact as the InAr[i] bit is probably diffrent, The important bits are that their not friends, They return refrences to the value so you can assign and retireve values, as you would in normal arrays. ( add your own bounds checking as approriate ).
If you wish to do other tricks in the operator set then up as approriate. The reason for a constant and a non const is for functions that require constant values.
|
|
|
|
04-06-2004, 10:05 PM
|
#3
|
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,368
Original Poster
Rep:
|
leonscape:
why is the & before operator and not after T? I know that & after T (T&) means T is passed as reference, what is the difference?
|
|
|
|
04-06-2004, 10:09 PM
|
#4
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
Nothing is diffrent, the placement is not important.
Code:
T& operator
T &operator
T & operator
All mean the same thing, its just a matter of style. I prefer it there because of things like
Code:
int* int_ptr, just_an_int;
If you notice even though the type looks like a pointer, the second value isn't a pointer. So I've got used to moving * and & next to the variable or function.
Code:
int *int_ptr, just_an_int, *int_ptr2;
Other people do it other ways.
|
|
|
|
04-06-2004, 10:15 PM
|
#5
|
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,368
Original Poster
Rep:
|
lol, I understood the "It doesn't matter they are the same" portion of that, but I am a C++ student and we havn't covered pointers yet, I learn about them next week in the class, and my personal studies experiance have not led me to learn about them on my own yet. thank you though :-D
|
|
|
|
04-06-2004, 10:22 PM
|
#6
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
Well & and * are used for pointers and refrences, very similar stuff, also to get a pointer from an int
Code:
int a = 5;
int *a_ptr = &a;;
So & means get the address of, So you can probably see why their related.
Last edited by leonscape; 04-06-2004 at 10:23 PM.
|
|
|
|
04-06-2004, 10:36 PM
|
#7
|
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,368
Original Poster
Rep:
|
yes that will come in handy next week, thank you :-D
|
|
|
|
04-06-2004, 11:21 PM
|
#8
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
I can't believe you've started class, and operator overloading before pointers. ( also the fact that arrays are also pointers ) Just seems odd to me.
|
|
|
|
04-06-2004, 11:48 PM
|
#9
|
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,368
Original Poster
Rep:
|
yeah, the teacher has been saying that both he and the author of our textbook is avoiding the subject of pointers, at first glance they seem unbeleavably simple, but you can screw yourself with them and undoubtedly will, plus c++ 1 and 2 require intro to programming that is taught in java... so we come in knowing about classes and arrays.
|
|
|
|
04-06-2004, 11:54 PM
|
#10
|
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,368
Original Poster
Rep:
|
ps.
ps:
also I have been taking java C#, C++ and bash all at the same time, so what I do not learn in one I learn in another then figure out for the rest with a quick read somewhere.
|
|
|
|
04-07-2004, 12:04 AM
|
#11
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
Oh you can do damage with pointers, ( and arrays ), but there the fastest, and easiest way to get a lot of work done. There not difficult, in fact there incredibly simple ( as it closely mirrors the way the machine actually works. )
In C++ there not as dangerous as in C ( new and delete are way better than malloc and free ). I just think its odd.
|
|
|
|
04-07-2004, 12:27 AM
|
#12
|
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,368
Original Poster
Rep:
|
well, if you say it is strange I guess it is, but oh well, as I said I am taking 3 programming clases (all 3 with same teacher and 5 min inbetween each in the same room X-|) and then bash shell scripting on the side. so I get a lot from everywhere.
|
|
|
|
04-17-2004, 03:06 PM
|
#13
|
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,368
Original Poster
Rep:
|
Just FYI, we covered pointers and I feel stupid that I didn't figure it out myself, thats pretty basic and simple.. thanx again.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:54 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|