operator char*()

R

Radde

Hi,


class String
{
public:
String(char* str);

void operator char*();

private:
char* data;
};

String::String()
{
if(str)
data = new char[strlen(str)+1];
strcpy(data, str);
}
};
operator char*()
{
return data;
}

void main()
{
string s1("HIHI");

const char* mystr = s1; //
}

One thing i want ask from above is
const char* mystr = s1; // s1.operator char*() gets called..
I really dont undertand, whats mechanism behind it..How does it
simplify to s1.operato char*()..

I know operator=() which takes one parameter as refernce and returns
reference object i.e

String& operator=(const String& s1);

suppose when we call
String s1("HII");
String s2("Hello");

s2 = s1, then it simplify to s2.operator=(s1)..
Similarly how does the above operator char*() works..i mean how is the
simplification done..

Cheers..
 
V

Victor Bazarov

Radde said:
class String
{
public:
String(char* str);

void operator char*();

private:
char* data;
};

String::String()

String::String(char* str)
{
if(str)

if (str) {
data = new char[strlen(str)+1];
strcpy(data, str);

}
operator char*()

void String::eek:perator char*()
{
return data;
}

void main()

int main()
{
string s1("HIHI");

String s1("HIHI");
const char* mystr = s1; //
}

This is awful. Please do NOT in the future post code by typing
it into your newsreader. ALWAYS copy-and-paste it from your C++
module that compiles.
One thing i want ask from above is
const char* mystr = s1; // s1.operator char*() gets called..
I really dont undertand, whats mechanism behind it..How does it
simplify to s1.operato char*()..

On the left of the assignment op you have 'mystr', which is of type
'const char*'. On the right you have something that needs to be of
type _assignable_ to 'const char*'. The compiler sees s1. What is
it to do? It is to find a conversion from 's1' to "something that
can be assigned to 'mystr'". It tries all possible things. Well,
the only thing, actually. That only thing is the user-defined
conversion from 'String' to 'char*'. 'char*' is compatible with
'const char*'. Voila.
I know operator=() which takes one parameter as refernce and returns
reference object i.e

String& operator=(const String& s1);

suppose when we call
String s1("HII");
String s2("Hello");

s2 = s1, then it simplify to s2.operator=(s1)..
Similarly how does the above operator char*() works..i mean how is the
simplification done..

It's magic.

V
 
R

Ram

Radde said:
Hi,


class String
{
public:
String(char* str);

void operator char*();

should be
operator char*();
private:
char* data;
};

String::String()
{
if(str)
data = new char[strlen(str)+1];
strcpy(data, str);
}
};
operator char*()
{
return data;
}

void main()
{
string s1("HIHI");

const char* mystr = s1; //
}

One thing i want ask from above is
const char* mystr = s1; // s1.operator char*() gets called..
I really dont undertand, whats mechanism behind it..How does it
simplify to s1.operato char*()..

When the compiler sees
const char *mystr = s1;
expression, it looks for a way to convert String into const char*. Had
you defined operator const char*() that would have get called, however
its also okay to construct a const char* from a char* for which the
operator char*() that you provided gets called.

Ram
 
V

Victor Bazarov

Sharad said:
Doesn't seem correct to me. Return type should not be specified on a
conversion function.

You're right. The return value type is erroneous. My mistake.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top