473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How to define operator "const char *" of enumerated type defined inside a class

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 surely this is
possible?

I could define an operator<< but for various reasons, I really want to
convert to a 'const char*' (to embed into a string which becomes part of a
window's caption, etc).

TIA,

Bill

class Class

{

public :

typedef enum {tt_SLBTool = 0, tt_DMM1, tt_ MAX 010} ToolTypeT;

// friend operator const char*(const Class::ToolTypeT &v); //
'operator const char *' must be a non-static member

// static operator const char*(const Class::ToolTypeT &v); //
'Class::operator const char *' must be a non-static member

};

// extern operator const char*(const Class::ToolTypeT &v); // 'operator
const char *' must be a non-static member

extern const char * Map( const Class::ToolTypeT &v); // But this is so ugly

This is what I want to write:

Class ClassObject;

..

const Class::ToolTypeT v=ClassObject.GetType();

CString str.

Str.Format("%s: version .",(const char*)v);

SetWindowText(str);
Sep 24 '08 #1
7 3027
Bill Davy wrote:
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 surely this is
possible?

I could define an operator<< but for various reasons, I really want to
convert to a 'const char*' (to embed into a string which becomes part of a
window's caption, etc).

TIA,

Bill

class Class

{

public :

typedef enum {tt_SLBTool = 0, tt_DMM1, tt_ MAX 010} ToolTypeT;

// friend operator const char*(const Class::ToolTypeT &v); //
'operator const char *' must be a non-static member

// static operator const char*(const Class::ToolTypeT &v); //
'Class::operator const char *' must be a non-static member

};

// extern operator const char*(const Class::ToolTypeT &v); // 'operator
const char *' must be a non-static member

extern const char * Map( const Class::ToolTypeT &v); // But this is so ugly

This is what I want to write:

Class ClassObject;

.

const Class::ToolTypeT v=ClassObject.GetType();

CString str.

Str.Format("%s: version .",(const char*)v);

SetWindowText(str);

Not sure what exactly is CString and how you define it, but this example
is doing what you want:

////////////////////////////////////
#include <iostream>
class a
{
public:

operator const char*();
};
a::operator const char* ()
{
return "a";
}
int main()
{
a obj;
const char* s = obj;
std::cout<<s<<std::endl;
}
Sep 24 '08 #2

"anon" <an**@no.invalidwrote in message
news:gb**********@news01.versatel.de...
Bill Davy wrote:
>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 surely this
is possible?

I could define an operator<< but for various reasons, I really want to
convert to a 'const char*' (to embed into a string which becomes part of
a window's caption, etc).

TIA,

Bill

class Class

{

public :

typedef enum {tt_SLBTool = 0, tt_DMM1, tt_ MAX 010}
ToolTypeT;

// friend operator const char*(const Class::ToolTypeT &v); //
'operator const char *' must be a non-static member

// static operator const char*(const Class::ToolTypeT &v); //
'Class::operator const char *' must be a non-static member

};

// extern operator const char*(const Class::ToolTypeT &v); // 'operator
const char *' must be a non-static member

extern const char * Map( const Class::ToolTypeT &v); // But this is so
ugly

This is what I want to write:

Class ClassObject;

.

const Class::ToolTypeT v=ClassObject.GetType();

CString str.

Str.Format("%s: version .",(const char*)v);

SetWindowText(str);


Not sure what exactly is CString and how you define it, but this example
is doing what you want:

////////////////////////////////////
#include <iostream>
class a
{
public:

operator const char*();
};
a::operator const char* ()
{
return "a";
}
int main()
{
a obj;
const char* s = obj;
std::cout<<s<<std::endl;
}
Thanks for this, but my goal is a conversion operator for an enumerated type
defined inside a class, not the class itself. But thanks for the
suggestion.

Rgds
Bill
Sep 24 '08 #3
Sam
Bill Davy writes:
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 surely this is
possible?
You can't really do this with merely an enum. You can, however, define a
class that, essentially, acts like an enum.

class Class {

public:

static const int a=0;
static const int b=1;
static const int c=2;

class ToolTypeT {
int v;

public:
ToolTypeT(int i): v(i) {}

ToolType &operator=(int i) { v=i; return *this; }

operator int() const { return v; }

operator const char *();
};
};

You can use this Class::ToolTypeT pretty much like an enumeration. Class::a,
Class::b, Class::c, etc. are your enumerator values. And, you can supply a
meaningful operator const char *().

And, with a few more tweaks, you can add additional type-safety to the whole
schema.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkjaIBAACgkQx9p3GYHlUOLREwCfX8LZDy9kK8 bIs12xIIWX3545
VLUAn0D0x/KLAbnmDYFCcDYC/KCsfQvY
=5Qvo
-----END PGP SIGNATURE-----

Sep 24 '08 #4

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 surely this
is
possible?
"Sam" <sa*@email-scan.comwrote in message
news:co****************************@commodore.emai l-scan.com...

You can't really do this with merely an enum. You can, however, define a
class that, essentially, acts like an enum.

class Class {

public:

static const int a=0;
static const int b=1;
static const int c=2;

class ToolTypeT {
int v;

public:
ToolTypeT(int i): v(i) {}

ToolType &operator=(int i) { v=i; return *this; }

operator int() const { return v; }

operator const char *();
};
};

You can use this Class::ToolTypeT pretty much like an enumeration. Class::a,
Class::b, Class::c, etc. are your enumerator values. And, you can supply a
meaningful operator const char *().

And, with a few more tweaks, you can add additional type-safety to the whole
schema.

Bill Davy writes:

That's an interesting approach. Can switch on ToolTypeT which is good. Can
even make a,b,c into an eunumeration (which may help some tools notice if I
have missed one out). And the type conversion is there.

Many thanks,
Bill
Sep 24 '08 #5
On Sep 24, 10:43 am, "Bill Davy" <B...@XchelSys.co.ukwrote:
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 surely this is possible?
It's not, and you almost certainly don't want to do it anyway.
I could define an operator<< but for various reasons, I really
want to convert to a 'const char*' (to embed into a string
which becomes part of a window's caption, etc).
The usual way of generating such strings is to use an
std::ostringstream. Anything else reeks of abuse or poor
design.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 24 '08 #6

"James Kanze" <ja*********@gmail.comwrote in message
news:67**********************************@f36g2000 hsa.googlegroups.com...
On Sep 24, 10:43 am, "Bill Davy" <B...@XchelSys.co.ukwrote:
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 surely this is possible?
It's not, and you almost certainly don't want to do it anyway.
I could define an operator<< but for various reasons, I really
want to convert to a 'const char*' (to embed into a string
which becomes part of a window's caption, etc).
The usual way of generating such strings is to use an
std::ostringstream. Anything else reeks of abuse or poor
design.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

---
So, I define operator<< and a template:

template string Stringify(const T &v)
{
ostringstream;
os << v << ends;
return os.str();
}

I'm not sure that's better than what I wanted to do, is it? A reader can
see what (const char*)t does and it is idiomatic.

I do not want to be accused of premature optimisation, but this solution
goes a long way round the houses (oops, another idiom) to turn 1 into "DMM".

But YMMV.

Bill

Sep 25 '08 #7
On Sep 25, 9:17 am, "Bill Davy" <B...@XchelSys.co.ukwrote:
"James Kanze" <james.ka...@gmail.comwrote in message
So, I define operator<< and a template:
template string Stringify(const T &v)
{
ostringstream;
os << v << ends;
return os.str();
}
Why a template?
I'm not sure that's better than what I wanted to do, is it?
A reader can see what (const char*)t does and it is idiomatic.
It's certainly not idiomatic, and it's already defined for enum
types---with a meaning that certainly isn't what you want.
(It's a reinterpret_cast.) In general, implicit conversions to
char const* (or to other pointer types) are to be avoided.
I do not want to be accused of premature optimisation, but
this solution goes a long way round the houses (oops, another
idiom) to turn 1 into "DMM".
It's not a question of optimization or whatever. The idiomatic
method of streaming data in C++ is to use operator<<, defining
operator<< for whatever types need streaming. Anything else is
obfuscation. That doesn't mean that it isn't worthwhile to
provide a general function for obtaining a symbolic value, as a
string, from an enum value---if nothing else, you could use it
to implement the operator<<. But the conversion shouldn't be
implicit. You want a named function.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 25 '08 #8

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

Similar topics

4
by: Radde | last post by:
Hi, class String { public: String(char* str); void operator char*();
5
by: Daniel Rudy | last post by:
What is the purpose of having a #define inside a struct? typedef struct { uByte bLength; uByte bDescriptorType; uWord wTotalLength; uByte bNumInterface;...
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...
11
by: vineoff | last post by:
struct C { operator const char*() { return "...."; } }; int main() { C a; 0; }
0
by: Sally Sally | last post by:
I am trying to figure out how I can change the default operator class of the index created for my primary key field. Is it even possible since I am not able to find the syntax? Or do I need to...
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...
1
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
2
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
4
by: =?Utf-8?B?cmtibmFpcg==?= | last post by:
How can I define type variant in c#?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.