I am a newbie and i am confused , If you can't understand this
question i am sorry, but i just want the clarification of this code.
Which compiler accepted that code?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Hi,
You are trying to multiply an integer with a string, which has no
meaning. You will get a compilation error.
Rgds,
Sumanth
The code you cite is not something you should waste your time with, it
seems to be part of an obfuscated program (intentionally written to be
hard to understand), or written by a real first-timer, or it may be
wrongly presented here.
int len = 0; len = 5;
This is identical to:
int len = 5;
The snippet:
"x41" * (len);
This is not valid in C++, you cannot multiply a const char array of
length 4 with an int. Old compilers for C and C++ may interpret this
as:
(int) "x41" * len;
This expression uses the memory address of the string constant "x41",
which may vary on each run of the program, and multiplies it with 5
(the content of the variable len). The round braces around len are
unneeded. Absent any instruction on what to do with the result of the
expression, it is then discarded - i.e. the whole line does nothing
and my be outright optimized away by the compiler. I assume there is
something missing in the code snippet you give.
best,
MiB.
well, i dont quite understand what you want to ask, nor what your code mean.
just from the code itself i thought:
"x41" is a const string, or, say the type is (char const *)
then the expression just got nothing meaningful.
#include<iostream>
#include<string>
using namespace std;
string operator* ( string str,unsigned int times){
string tmp;
for( unsigned int i=0; i<times; i++) tmp+=str;
return tmp;
}
int main()
{
string str="hello,";
cout<<str*5<<endl;
return 0;
}
____________compiled by gcc 4.30_____
traxex # string_mul.exe
hello,hello,hello,hello,hello,
traxex #