question

May 11, 2015 at 3:49am
hello friends, i have this code, i know it is not complite even but i try to help my self to understand how supscript operator overloading work, so can you please help me to complitle this code as simple idea just to make it clear?
thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<string>
using namespace std; 
class person{
string name;
int age;
public:
	person(){}
person(string n,int a){
name=n;
age=a;
}
int getage(){return age;}
string getname(){return name;}

int& operator[](int index){
person obj;
if(index==[0])
	return 
else
	if(index==(1))
		cout<<obj.getname();
}

};
 int main()
 {
	
person objj;
cout<<"the index for friest one is "<<objj[1]<<endl;
cout<<"the index for secound index is "<<objj[1];
system("pause");
   return 0;
}
May 12, 2015 at 8:24pm
any help?
May 12, 2015 at 9:16pm
Don't worry about operator overloading just yet, you're still missing some of the basics. For instance, if you want your variable 'objj' to be an array, then why didn't you make it as such? Or are you just doing something intentionally wacky with the overloaded operator?

Also, you are trying to extract meaning full data from uninitialized fields. That will never work.
May 13, 2015 at 9:55am
yes i made this array cuz i just try to call the operator ,do you have any example just to understand this operator ?
May 13, 2015 at 1:09pm
can anyone suggest any result for this code ,i mean this code just to understand how i should vall in inside main
May 13, 2015 at 2:10pm
You never set the member variables in your class to anything. So you'll never get anything meaningful regardless of your code.
May 13, 2015 at 2:18pm
if you want to make just an array you make it like with every other type:
person arr[5];
If you want to overload operator you make it like so:
return-type operator(type of operator without parentheses)(one or two arguments depending on operator);
e.g.
1
2
unsigned operator+(person, unsigned);
unsigned operator*(person); // it's not multiplying, you apply it like this: *some_type 

Edit:
never use system()! It's BAD!
Last edited on May 14, 2015 at 4:22pm
May 13, 2015 at 2:34pm
never use system()! It's BAD!

Never say "never use X", it's lazy and unhelpful. While it's true that the "system()" function is mostly seen as a boondoggle for the lazy and un-productive, you have to sort of except that the people that wrote and upkeep these libraries are smarter then you and me so there is a reason that it exists. Even if that reason is as dumb as "Somebody someday might want to use it.", it is still one that they, the C++ committee, have excepted as being good enough.
May 13, 2015 at 6:08pm
The system() is in C-library, and thus the C++ committee hasn't had much say on the matter.

Why "bad"?
http://www.cplusplus.com/articles/j3wTURfi/


There can be legitimate reasons to execute other commands and do it with system() rather than one of the other similar functions, but calling the program "pause" it not one of them.
May 13, 2015 at 7:50pm
If it was so good, anti-viruses wouldn't complain on software containing it. I know it's sometimes (I mean very rarely, hardly ever) useful. If you want to use it, check, if there is no other way with api. If there is no alternative you can use it (it's kind of like with goto, though goto is sometimes better alternative, e.g. nested loops).
May 13, 2015 at 8:39pm
@ TheHardew: I'm not attacking you, not intentionally. What I want to do is stamp out this 'fan-boyism'\bandwagon crap that saturates every other corner of the internet. It only serves to make users dumber. If you had said not to use "system()" because you don't have as much control over the child object as you would otherwise, or that all you get back is an error code and even then it's from the child app instead of the function that actually failed, or that it halts execution on your process until the child finishes or a myriad of other legitimate reasons that this function sucks then I would have agreed with you and left it alone. But a statement like the one you made without context doesn't help OP very much, and as you can see by their code they have more important things to worry about then other peoples pet-peeves.

The reason that calling system("pause"); is stupid by the way is that console programs are meant to be run in a console environment. Programs that happen to run in the console shell that are executed from a graphical desktop are not meant to preserve output, or if they are it's done to a secondary stream such as a log file. The point is that there is no need to "Hold the screen open so you can see what happened." because a command shell does that all by itself.

AV suites don't complain about "system()" btw. Only one, maybe two, of them ever really did and even that was for such a short while and such a long time ago that it's hardly worth mentioning. Heuristic detection schemes are a lot more complicated then just axing a program because of one black-balled function. Especially a function like this that has virtually no practical use for exploits when compared to alternative approaches.
Last edited on May 13, 2015 at 8:40pm
May 14, 2015 at 4:20pm
Ok, thanks and sorry, I didn't plan on attacking you either. Next time I'll write why it is so bad. Just after seeing examples of finding whether a directory exists or not with system() I forgot, that rarely it's got some sense.
Topic archived. No new replies allowed.