Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13035
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| 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-03 13:50 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jdvimd$d7a$1@dont-email.me> (permalink) |
| References | <5e549254-2bb6-40e7-aafe-55a9bbca691e@v24g2000yqk.googlegroups.com> |
On 1/3/2012 1:00 PM, avasilev wrote:
> I have the following class method:
>
> class MyClass
> {
> public:
> static const char* typeName() { return "myClass";}
> };
> The pointer that this method returns serves as an unique identifier of
> the class. Thus, it must not only point to the class name string, but
> also the value of the pointer itself serfes as an UID.
>
> My question is: Since this method is defined inside the class
> declaration, in a header file, is it guaranteed that the same
> 'version' of the method will be called in all compilation units in my
> application, and any call to the method will return exactly the same
> pointer, or will there be different 'copies' of the method and string
> literal in each compilation unit, and they will return a different
> char* ?
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; }
};
V
--
I do not respond to top-posted replies, please don't ask
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