LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-06-2004, 09:41 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
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.
 
Old 04-06-2004, 09:56 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
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.
 
Old 04-06-2004, 10:05 PM   #3
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
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?
 
Old 04-06-2004, 10:09 PM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
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.
 
Old 04-06-2004, 10:15 PM   #5
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
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
 
Old 04-06-2004, 10:22 PM   #6
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
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.
 
Old 04-06-2004, 10:36 PM   #7
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
yes that will come in handy next week, thank you :-D
 
Old 04-06-2004, 11:21 PM   #8
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
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.
 
Old 04-06-2004, 11:48 PM   #9
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
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.
 
Old 04-06-2004, 11:54 PM   #10
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
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.
 
Old 04-07-2004, 12:04 AM   #11
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
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.
 
Old 04-07-2004, 12:27 AM   #12
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
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.
 
Old 04-17-2004, 03:06 PM   #13
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
Just FYI, we covered pointers and I feel stupid that I didn't figure it out myself, thats pretty basic and simple.. thanx again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# with dotGNU, operator overloading exodist Programming 2 03-15-2004 05:52 PM
Builtin operator overloading. exodist Programming 15 03-11-2004 10:41 PM
c++ overloading input operator true_atlantis Programming 4 02-25-2004 07:24 PM
Operator Overloading - Java? Tesl Programming 4 02-13-2004 12:54 AM
C++ overloading extraction operator petercool Programming 5 09-06-2003 06:35 AM


All times are GMT -5. The time now is 10:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration