Problems with Conversion function

K

Kailang Deng

Hi,everybody.
i have a program from More Effective C++ as follows(The code
irrelevant is taken out).
And i am puzzled by the outputs.

class String
{
public:
class CharProxy //proxies for string chars
{
public:
CharProxy(String& str,int index); //constructor
operator char() const
{
cout<<string.data[CharIndex]<<endl; //prints 'r'
return string.data[CharIndex];
}
private:
String& string;
int CharIndex;
};
friend class CharProxy;
String(const char *value=""); //default constructor
String(const String& rhs); //copy constructor
CharProxy operator[] (int index) //subscript operator
{
return CharProxy(*this,index);
}
private:
char *data;
};
int main()
{
String s1("More");
cout<<s1[2]<<endl; //prints 114,not 'r'. why?
return 0;
};

Can anyone explain the output? Thank you very much.
Best Regards.
-Bruce
 
B

Brice Gagnage

Hi,everybody.
i have a program from More Effective C++ as follows(The code
irrelevant is taken out).
And i am puzzled by the outputs.

class String
{
public:
class CharProxy //proxies for string chars
{
public:
CharProxy(String& str,int index); //constructor
operator char() const
{
cout<<string.data[CharIndex]<<endl; //prints 'r'
return string.data[CharIndex];
}
private:
String& string;
int CharIndex;
};
friend class CharProxy;
String(const char *value=""); //default constructor
String(const String& rhs); //copy constructor
CharProxy operator[] (int index) //subscript operator
{
return CharProxy(*this,index);
}
private:
char *data;
};
int main()
{
String s1("More");
cout<<s1[2]<<endl; //prints 114,not 'r'. why?
return 0;
};

Can anyone explain the output? Thank you very much.
Best Regards.
-Bruce

Well, s1[2] returns a ChaRProxy. This means the operator<<
has been defined somewhere to manage this kind of affectation.
I think you should check how it has been defined, it probably
add the char as if it is a number (114 beeing the ASCII value for 'r').

Cheers,
 
K

Kai-Uwe Bux

Kailang said:
Hi,everybody.
i have a program from More Effective C++ as follows(The code
irrelevant is taken out).

The error is in the part taken out.
And i am puzzled by the outputs.

I am puzzled by the output you get: after supplying the missing parts to
make a compilable program, I get 'r' in both places.

[snip incomplete code]
Can anyone explain the output?

I doubt it as nobody will be able to reproduce your results. Chances are
that you have undefined behavior elsewhere in the program.

Please post a minimal, yet complete program that demonstrates the problem.


Best

Kai-Uwe Bux
 
K

Kailang Deng

Kailang said:
Hi,everybody.
i have a program from More Effective C++ as follows(The code
irrelevant is taken out).

The error is in the part taken out.
And i am puzzled by the outputs.

I am puzzled by the output you get: after supplying the missing parts to
make a compilable program, I get 'r' in both places.

[snip incomplete code]
Can anyone explain the output?

I doubt it as nobody will be able to reproduce your results. Chances are
that you have undefined behavior elsewhere in the program.

Please post a minimal, yet complete program that demonstrates the problem.
Thank you for your help.
It's my fault.I intent to make the demonstration brief by omitting
some "irrelevant" code. Now it seems to make things worse. o($B"A(B_$B"A(B)o...
Here is the complete compilable program.

#include<iostream>
#include<string>
using namespace std;

class String
{
public:
class CharProxy //proxies for string chars
{
public:
CharProxy(String& str,int index):string(str),CharIndex(index) //
constructor
{}
operator char() const
{
cout<<string.data[CharIndex]<<endl; //prints 'r'
return string.data[CharIndex];
}
private:
String& string;
int CharIndex;
};
friend class CharProxy;
String(const char *value="") //default constructor
{
data=new char[strlen(value)+1];
strcpy(data,value);
}
String(const String& rhs) //copy constructor
{
data=new char[strlen(rhs.data)+1];
strcpy(data,rhs.data);
}
CharProxy operator[] (int index) //subscript operator
{
return CharProxy(*this,index);
}
private:
char *data;
};

int main()
{
String s1("More");
cout<<s1[2]<<endl; //prints 114. why?

return 0;
};


Best Regards
 
J

Joe Smith

Kailang said:
Thank you for your help.
It's my fault.I intent to make the demonstration brief by omitting
some "irrelevant" code. Now it seems to make things worse. o($B"A(B_$B"A(B)o...
Here is the complete compilable program.
That code prints, 'r' twice for me, so something weird is going on. What
compiler are you using?
What compiler options are you using?

(This sounds like it will be a compiler specific problem, which is
technically off-topic for this group. That said, you could not know that
when you first posted, and you've already posted here, so we might as well
finish tracking down the odd behavior you are seeing.)
 
K

Kailang Deng

That code prints, 'r' twice for me, so something weird is going on. What
compiler are you using?
What compiler options are you using?

I am using MS Visual C++ 6.0 and keep the default compiler options.
(This sounds like it will be a compiler specific problem, which is
technically off-topic for this group. That said, you could not know that
when you first posted, and you've already posted here, so we might as well
finish tracking down the odd behavior you are seeing.)

which group shoud these compiler specific problem be posted? Do you
have any advices?
Thank you ~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top