473,473 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

operator const char*()

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

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

Nov 22 '05 #1
11 6909
vineoff wrote:
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

Nov 22 '05 #2
In c language, a[0] and 0[a] is equivalent.

Nov 22 '05 #3
Yes, a[0] calls that operator too, but why?

Nov 22 '05 #4
vineoff wrote:
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.

Nov 22 '05 #5
Why is it const char*, why not i.e. int ?

Nov 22 '05 #6
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

Nov 22 '05 #7
vineoff wrote:
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.

Nov 22 '05 #8
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?

Nov 22 '05 #9
vineoff wrote:
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
Nov 22 '05 #10
vineoff wrote:
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

Nov 22 '05 #11
Yes of course, the point wasn't actually that.

Nov 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Fausto Lopez | last post by:
I'm getting the following error: 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *' when I try to compile the following code: HRESULT AnsiToUnicode(CString pszA,...
2
by: TonyM | last post by:
Q: Is there a good way to overcome this apparent bug without modifying the mfc code? ___________________________________ Info: Although it is NOT a good idea, I seemed to have perhaps located a...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
11
by: sadegh | last post by:
I wrote a function to convert any type to string: template <typename T> std::string toStr(const T &thing) { std::ostringstream outStr; outStr << thing; return outStr.str(); } the above...
14
by: Javier | last post by:
Hello, in which cases is it better the use of "const char*" to "string" (or even const string &). I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see: hash_map<const char*, int,...
10
by: Erik Knudsen | last post by:
Hi! In converting applications from ansi to unicode, it is a problem that the std::wcout accepts a const char * without complaining compile time. This compiles and runs:...
13
by: S S | last post by:
Hi I have a very basic question, but it's a good one. Below is the code fragment. struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0;
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.