Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What Is The Difference Between CMyClass* and CMyClass&?

1 view
Skip to first unread message

Landon

unread,
Oct 14, 2008, 8:09:01 PM10/14/08
to
I use MFC Visual C++ 4.2.

1. I would like to know the difference between CMyVariable&( CString, int,
etc.. ) and CMyVariable*?

2. And for class:
What is the difference between CMyClass& and CMyClass*?

Thank you.

Jonathan Wood

unread,
Oct 14, 2008, 8:36:26 PM10/14/08
to
They are both similar in that they reference a variable of the specified
type.

The main difference is that you need to use pointer syntax with the second
version (var->member).

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:FD16C2DD-3A2A-407E...@microsoft.com...

Landon

unread,
Oct 14, 2008, 8:48:16 PM10/14/08
to
So how can I know when to use * and when to use &?

And also what is the difference with CMyClass only without anything?

Thank you.

Mark Salsbery [MVP]

unread,
Oct 14, 2008, 8:48:21 PM10/14/08
to

"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:FD16C2DD-3A2A-407E...@microsoft.com...

& is the reference operator
http://msdn.microsoft.com/en-us/library/w7049scy(VS.71).aspx

* indicates a pointer
http://msdn.microsoft.com/en-us/library/89e4h321.aspx

These concepts are fundamental to C++ - I would really recommend studying
them until you know exactly what's going on and how to use them :)

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++


Joseph M. Newcomer

unread,
Oct 14, 2008, 9:21:54 PM10/14/08
to
If the value can be a NULL pointer, you have to use *. Otherwise, it is generally better
practice to use &. For example, suppose I want a function that can work on any edit
control that I have handy. I could write
void CMyFunction(CEdit * x) { ... }
or
void CMyFuncion(CEdit & x) { ...}

I prefer the & because I don't have to use the silly -> everywhere (a poor language design
issue caused by a poor language design in 1975 that continues to be a perpetual annoyance)
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Jonathan Wood

unread,
Oct 15, 2008, 1:38:22 AM10/15/08
to
I don't disagree, however, I've had clients who prefer pointer syntax just
because it makes it apparent from the call that a pointer is being passed
and, therefore, the object coult be modified. (Not as much an issue with
CEdit.)

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:t8haf4le8hubgbo33...@4ax.com...

Jonathan Wood

unread,
Oct 15, 2008, 1:40:40 AM10/15/08
to
"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:DF77B83D-7CEB-4C05...@microsoft.com...

> And also what is the difference with CMyClass only without anything?

This is completely different. This syntax actually creates a new instance of
the specified type, while * and & reference an existing instance.

This is pretty fundamental C++. If this type of stuff is unclear, I'm not
sure you're ready to take on a large project. Rather, you should be working
your way through some books and creating small test programs.

Doug Harrison [MVP]

unread,
Oct 15, 2008, 8:37:03 PM10/15/08
to
On Tue, 14 Oct 2008 23:38:22 -0600, "Jonathan Wood"
<jw...@softcircuits.com> wrote:

>I don't disagree, however, I've had clients who prefer pointer syntax just
>because it makes it apparent from the call that a pointer is being passed
>and, therefore, the object coult be modified. (Not as much an issue with
>CEdit.)

Regarding the "call-site clue" and still more on when to use a pointer vs.
a reference, you might want to refer them to this message:

http://groups.google.com/group/microsoft.public.vc.mfc/msg/d9fc9e8697f860d6?hl=en

--
Doug Harrison
Visual C++ MVP

Joseph M. Newcomer

unread,
Oct 15, 2008, 10:16:59 PM10/15/08
to
If it is intended to be modified, it is a Whatever &, and if it is not to be modified, it
is a const Whatever &. So there is no need to use silly things like "pointer objects can
be modified", after all, it could be a const Whatever *, so the & at the call site serves
no useful purpose other than catering to the habits of 1975 PDP-11 programmers.
joe

On Tue, 14 Oct 2008 23:38:22 -0600, "Jonathan Wood" <jw...@softcircuits.com> wrote:

>I don't disagree, however, I've had clients who prefer pointer syntax just
>because it makes it apparent from the call that a pointer is being passed
>and, therefore, the object coult be modified. (Not as much an issue with
>CEdit.)

Jonathan Wood

unread,
Oct 16, 2008, 12:03:22 AM10/16/08
to

"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:eu8df4tknuh6jdnl1...@4ax.com...

> If it is intended to be modified, it is a Whatever &, and if it is not to
> be modified, it
> is a const Whatever &. So there is no need to use silly things like
> "pointer objects can
> be modified", after all, it could be a const Whatever *, so the & at the
> call site serves
> no useful purpose other than catering to the habits of 1975 PDP-11
> programmers.

The client who felt this way was not stupid, or "silly." Although I don't
apply the same rules to my own code, I can see the benefit of it.

In the practical world, I've seen cases where it added clarity to what was
happening. I don't need any more theory than that.

0 new messages