operator const char*()

V

vineoff

struct C
{
operator const char*() { return "...."; }
};
int main()
{
C a;
0[a];
}

Why does that 0[a] call operator const char*() ?
 
J

Jonathan Mcdougall

vineoff said:
struct C
{
operator const char*() { return "...."; }
};
int main()
{
C a;
0[a];
}

Why does that 0[a] call operator const char*() ?

Not sure, but here's a guess.

Take for example e1[e2]. This is exactly like *(e1 + e2). Since
addition is commutative, this is the same as *(e2 + e1), which is again
equivalent to e2[e1]. I suppose since the compiler cannot subscript an
integer, it reverses the subscript, making it

a[0];

and converts 'a' to a const char*.


Jonathan
 
R

Rolf Magnus

vineoff said:
Yes, a[0] calls that operator too, but why?

How else would the compiler resolve the [] operator? Your struct C has none
defined, so the only way is to convert C to a const char* and then use
operator[] for that type.
 
J

Jonathan Mcdougall

vineoff wrote:

Please quote the message you are answering to.
Why is it const char*, why not i.e. int ?

Why would it be an int?

The compiler tries many different ways to accomodate what you type.
With

class C
{
public:
operator const char*();
};

int main()
{
C c;
c[0];
}

the compiler checks whether c
1) is a pointer, or
2) has a class type which defines operator[], or
3) has a class type which can be converted to another type which is
either 1), 2) or 3)

(non exhaustive)

Your class C is case 3). It is converted to const char* which is a
pointer.


Jonathan
 
R

Rolf Magnus

vineoff said:
Why is it const char*,

Because that's the conversion you provided. You didn't define any other.
why not i.e. int ?

int cannot be dereferenced, but a pointer can. And also, you didn't provide
a conversion to int.
 
V

vineoff

Okay.

Next question.

struct C
{
operator int*() { int a = 54; return &a; }
};

int main()
{
C a;
std::cout<<a[0]<<a[0];
}


Why does that print 54 at the first time?
 
J

John Harrison

vineoff said:
Okay.

Next question.

struct C
{
operator int*() { int a = 54; return &a; }
};

int main()
{
C a;
std::cout<<a[0]<<a[0];
}


Why does that print 54 at the first time?

This program has undefined behaviour because you are deferenceing an
address of an object that no longer exists. It might print 54 for you
but there is no reason that it should print 54 for anyone else.

A program with undefined behaviour could do anything.

If you change it to this (note the addition of 'static')

struct C
{
operator int*() { static int a = 54; return &a; }
};

int main()
{
C a;
std::cout<<a[0]<<a[0]<<'\n';
}


The the program has defined behaviour and I would expect it to print 5454.

john
 
G

Greg

vineoff said:
Okay.

Next question.

struct C
{
operator int*() { int a = 54; return &a; }
};

int main()
{
C a;
std::cout<<a[0]<<a[0];
}


Why does that print 54 at the first time?

Generally it's a better idea not to write conversion methods (since
their behavior can be surprising as the above program shows). Instead,
I would overload particular operators where doing so make sense.

In this case, overloading the [] (subscript) operator lets the program
return a result by value. And values are less like to cause problems
than pointers (at least of the kind that the program above
demonstrates):

#include <iostream>

struct C
{
int operator[](int) const { return 54; }
};

int main()
{
C a;

std::cout << a[0] << a[0];
}

Greg
 

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
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top