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

Home Posts Topics Members FAQ

What does the object name represent?

The following has recently arose and it is the first time I have noticed
this behaviour.

I have been using a self-defined string class. It's form is

class CMyString
{
public:
... (no public varaibles)
...
operator char* () const { return mpChar; }

private:
char* mpChar;
unsigned long mBytesAllocated;
};
Obviously the purpose of the casting operator is to be able to use the
object much like a null terminated c-string.

The following code is ok:

char Buff[100];
CMyString MyString("Hello");

strcpy(Buff, (char*) MyString);
Something interesting happens if the cast operator is (accidently) omitted.

strcpy(Buff, MyString);
This code compiles and links without warning. Lint does warn that the second
argument is not a pointer.
However, this code executes and Buff end up with the value "Hello". It
appears that the object name is interpreted
as a pointer to the mpChar private data member. If the specificiation order
of mpChar and mBytesAllocated
is reversed, the code again compiles and links but will crash on execution.

My conclusion is that the name of an object is a pointer to the first(?)
class data member. I have never seen this
mentioned in the literature. Is this common knowledge and I have just missed
it? I would not have immediately
caught this without lint and, in some ways, it appear dangerous.

Thanks for any comments.

Phil Ward


Nov 2 '06 #1
5 1640
Phil Ward wrote:
The following has recently arose and it is the first time I have
noticed this behaviour.

I have been using a self-defined string class. It's form is

class CMyString
{
public:
... (no public varaibles)
...
What does a c-tor look like? Did you follow "The Rule of Three"?
operator char* () const { return mpChar; }
Bad idea. You should do

operator const char*() const { reutrn mpChar; }

otherwise you are opening a door to modify the "contents" of an object
you deem 'const'.
>
private:
char* mpChar;
unsigned long mBytesAllocated;
};
Obviously the purpose of the casting operator is to be able to use the
object much like a null terminated c-string.

The following code is ok:

char Buff[100];
CMyString MyString("Hello");

strcpy(Buff, (char*) MyString);
Something interesting happens if the cast operator is (accidently)
omitted.
strcpy(Buff, MyString);
This code compiles and links without warning. Lint does warn that the
second argument is not a pointer.
However, this code executes and Buff end up with the value "Hello". It
appears that the object name is interpreted
as a pointer to the mpChar private data member. If the specificiation
order of mpChar and mBytesAllocated
is reversed, the code again compiles and links but will crash on
execution.
That's more interesting. Have you tried investigating?
My conclusion is that the name of an object is a pointer to the
first(?) class data member.
Huh? Never heard of it. Your conversion function is used. But it
should be used in both cases.
I have never seen this
mentioned in the literature. Is this common knowledge and I have just
missed it? I would not have immediately
caught this without lint and, in some ways, it appear dangerous.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 2 '06 #2
Ok - I think I have partially figured out. It seems that when performing

strcpy(Buff, MyString), that the compiler is employing an implicit cast
using the
(char*) operator. That is neat, but I will have to bone up on implicit
casting.

My appologies, but the crash I described earlier is incorrect.

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:ei**********@news.datemas.de...
Phil Ward wrote:
>The following has recently arose and it is the first time I have
noticed this behaviour.

I have been using a self-defined string class. It's form is

class CMyString
{
public:
... (no public varaibles)
...

What does a c-tor look like? Did you follow "The Rule of Three"?
> operator char* () const { return mpChar; }

Bad idea. You should do

operator const char*() const { reutrn mpChar; }

otherwise you are opening a door to modify the "contents" of an object
you deem 'const'.
>>
private:
char* mpChar;
unsigned long mBytesAllocated;
};
Obviously the purpose of the casting operator is to be able to use the
object much like a null terminated c-string.

The following code is ok:

char Buff[100];
CMyString MyString("Hello");

strcpy(Buff, (char*) MyString);
Something interesting happens if the cast operator is (accidently)
omitted.
strcpy(Buff, MyString);
This code compiles and links without warning. Lint does warn that the
second argument is not a pointer.
However, this code executes and Buff end up with the value "Hello". It
appears that the object name is interpreted
as a pointer to the mpChar private data member. If the specificiation
order of mpChar and mBytesAllocated
is reversed, the code again compiles and links but will crash on
execution.

That's more interesting. Have you tried investigating?
>My conclusion is that the name of an object is a pointer to the
first(?) class data member.

Huh? Never heard of it. Your conversion function is used. But it
should be used in both cases.
>I have never seen this
mentioned in the literature. Is this common knowledge and I have just
missed it? I would not have immediately
caught this without lint and, in some ways, it appear dangerous.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Nov 2 '06 #3
On Thu, 02 Nov 2006 18:12:44 GMT in comp.lang.c++, "Phil Ward"
<ph*********@mchsi.comwrote,
>Ok - I think I have partially figured out. It seems that when performing

strcpy(Buff, MyString), that the compiler is employing an implicit cast
using the
(char*) operator. That is neat, but I will have to bone up on implicit
casting.
That is exactly what you are asking for by defining that operator.
If you want to access the pointer explicitly only (highly recommended)
then define a member function with some name that you choose, like
c_str(), that cannot bite you implicitly.
>My appologies, but the crash I described earlier is incorrect.
You crashed because you strcpy() something to the class's buffer without
preparing to receive it. To help avoid that, if you keep the implicit
cast operator, at minimum make it

operator char const* () const;

Nov 2 '06 #4

Phil Ward wrote:
The following has recently arose and it is the first time I have noticed
this behaviour.

I have been using a self-defined string class. It's form is

class CMyString
{
public:
... (no public varaibles)
...
operator char* () const { return mpChar; }

private:
char* mpChar;
unsigned long mBytesAllocated;
};
Obviously the purpose of the casting operator is to be able to use the
object much like a null terminated c-string.

The following code is ok:

char Buff[100];
CMyString MyString("Hello");

strcpy(Buff, (char*) MyString);
Something interesting happens if the cast operator is (accidently) omitted.

strcpy(Buff, MyString);
What's so interesting about it? There is a prototype of strcpy in
scope, which is:

char *strcpy(char *dest, const char *src);

MyString is an object of a class which has a char * operator, giving
rise for a way for that object to be converted to the parameter type.
This code compiles and links without warning. Lint does warn that the second
argument is not a pointer.
Find a properly written lint that understands C++ classes and
conversion rules.
My conclusion is that the name of an object is a pointer to the first(?)
class data member.
That's a silly conclusion, given that the class has an "operator char
*()" function staring you in the face, which you even reproduced for
the benefit of the newsgroup.

Nov 2 '06 #5
Phil Ward wrote:
Ok - I think I have partially figured out. It seems that when performing

strcpy(Buff, MyString), that the compiler is employing an implicit cast
using the
(char*) operator. That is neat, but I will have to bone up on implicit
casting.
There is no such thing as an "implicit cast". It's simply a conversion.
A cast is an explicit conversion.

Implicit conversions are everywhere. The concept is inherited into C++
from C.

For instance:

int x = 3;
long y = 4;
double z = x + y;

Here, x is implicitly converted to type long, making it compatible for
addition with y. Then the result of the expression is implicitly
converted to double, so it can be assigned to z.

C++ provides ways for automatic conversion to and from class types
under programmer control, so that the same notational convenience can
be achieved when using objects.

Nov 2 '06 #6

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
112
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please,...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
3
by: Sathyaish | last post by:
I wanted to practice some Linked List stuff, so I set out to create a linked list. The plan was to create the following: (1) A linked list class in Visual Basic (2) A non-class based linked list...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
13
by: Kyle Adams | last post by:
I don't know where is the right place to ask this so I will start here. Can someone explain to me what these represent? I think they all have to do with the middleware level, but I really don't...
5
by: Ryan | last post by:
{"POINTID":77902,"MAPID":762,"LONG":-122.21654892,"LAT":"37.1834331019","CITY":"Boulder Creek","STATE":"CA","DIST":5745.4} I get an "invalid label" error... I'm kinda new to this. Thanks!
82
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
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
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?

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.