Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13039
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: char* to literal return from static in-class method definition - will the pointer always be the same? |
| Date | 2012-01-04 08:37 +1300 |
| Message-ID | <9mh3nmFpeqU1@mid.individual.net> (permalink) |
| References | <5e549254-2bb6-40e7-aafe-55a9bbca691e@v24g2000yqk.googlegroups.com> <jdvimd$d7a$1@dont-email.me> <47e4778c-8a9e-40c7-ac32-a562ba60b4fe@z25g2000vbs.googlegroups.com> |
On 01/ 4/12 08:17 AM, avasilev wrote:
Please don't snip attributions.
>> Don't compare pointers. Compare the string contents. If you want to
>> ensure that the pointers are always the same, create an object and
>> return its address. There is no guarantee that two literals have the
>> same address if their contents match. It's *usually* the case since the
>> linker merges those. The language doesn't require it, though.
>>
>> Probably better to do
>>
>> class MyClass
>> {
>> public:
>> static char const* typeName() {
>> static const char name[] = "myClass"; return name; }
>> };
>>
>
> Let me clarify a bit more on the use case. I actually have only one
> place in code where the literal is - namely the typeName()method. The
> class can be regarded as a factory, which creates objects, which have
> a char pointer field, which the factory assigns to the return value of
> typeName(). Later, when a function is called, taking such an object as
> argument, it does type checking by calling typeName() of the factory,
> and comparing the returned pointer to the object's field.
That's why Victor suggested a local static object: the address will be
unique.
--
Ian Collins
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
char* to literal return from static in-class method definition - will the pointer always be the same? avasilev <alxvasilev@gmail.com> - 2012-01-03 10:00 -0800
Re: char* to literal return from static in-class method definition - will the pointer always be the same? Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-03 13:50 -0500
Re: char* to literal return from static in-class method definition - will the pointer always be the same? avasilev <alxvasilev@gmail.com> - 2012-01-03 11:04 -0800
Re: char* to literal return from static in-class method definition - will the pointer always be the same? avasilev <alxvasilev@gmail.com> - 2012-01-03 11:17 -0800
Re: char* to literal return from static in-class method definition - will the pointer always be the same? Ian Collins <ian-news@hotmail.com> - 2012-01-04 08:37 +1300
Re: char* to literal return from static in-class method definition - will the pointer always be the same? avasilev <alxvasilev@gmail.com> - 2012-01-03 11:07 -0800
Re: char* to literal return from static in-class method definition - will the pointer always be the same? Goran <goran.pusic@gmail.com> - 2012-01-03 23:47 -0800
Re: char* to literal return from static in-class method definition - will the pointer always be the same? avasilev <alxvasilev@gmail.com> - 2012-01-04 04:15 -0800
Re: char* to literal return from static in-class method definition - will the pointer always be the same? Goran <goran.pusic@gmail.com> - 2012-01-05 00:30 -0800
Re: char* to literal return from static in-class method definition - will the pointer always be the same? Victor Bazarov <v.bazarov@comcast.invalid> - 2012-01-04 08:43 -0500
csiph-web