Am I being thick?

Am I being thick?

Post by Henr » Wed, 11 Feb 2004 00:57:10


I'm neither an expertin C++ nor MFC programming, but the following lines of
code seems straight foward, but returns with an

error 2100: illegal indirection.

// create new CReference object
CReference *p_reference = (CReference*) new CReference;
// copy from addreference that belongs to a dialog
*p_reference = (CReference)adddia.addreference;
// sets internal integer value of p_reference., but causes a compilation
error
*p_reference->m_iID = 1;

If it makes a different, p_reference is later inserted into a COBArray

I actually managed to get round it doing adddia.addreference.m_iID = 1
before copying. But it's all a bit strange to me.

regards
H
 
 
 

Am I being thick?

Post by Bjarne Nie » Wed, 11 Feb 2004 01:24:59

See inline

"Henry" < XXXX@XXXXX.COM > skrev i en meddelelse

of
*****
It's not necessary to cast to the same type
*****

compilation
*****
Either call it like
*p_reference.m_iID = 1;
or
p_reference->m_iID = 1;
*****


--
Bjarne Nielsen

 
 
 

Am I being thick?

Post by Ali R » Wed, 11 Feb 2004 01:28:49

Which line is the error on????

Anyway, you don't need to cast your new statement the return type and the
variable are the same

CReference *p_reference = new CReference;

Now as far as the assignment goes, does your CReference class have an =
operator?

*p_reference = (CReference)adddia.addreference;

Ali R.



of
compilation
 
 
 

Am I being thick?

Post by Ali R » Wed, 11 Feb 2004 01:32:30

And I missed the way you were accessing the variable inside p_reference.

either use a *p_reference.variable or p_reference->variable

Ali R.
 
 
 

Am I being thick?

Post by Paavo Held » Wed, 11 Feb 2004 02:01:14


-> bounds tighter than *, so you have to use parens:

(*p_reference)->m_iID = 1;

hth
Paavo
 
 
 

Am I being thick?

Post by Paavo Held » Wed, 11 Feb 2004 02:06:22


Sorry, pressed the send button too soon. Actually you have double
indirection here, but I suspect you only need one. You can write either

p_reference->m_iID = 1;

or

(*p_reference).m_iID = 1;

Paavo
 
 
 

Am I being thick?

Post by Dan Bloomq » Wed, 11 Feb 2004 02:23:49


You can:
CReference *p_reference= new CReference;

if you look at it like this:
CReference* p_reference= new CReference;

you will see that p_reference is a pointer to the object.



if addreference is a CReference object, you only need to:
*p_reference= adddia.addreference;

You only need to cast if CReference is a base class of addreference. It
is always safer not to cast unless you absolutely know what you intend.


p_reference->m_iID = 1;
or:
(*p_reference).m_iID = 1;

But:
*p_reference.m_iID = 1;

will not work. the (.) takes precedence over (*) and tries to treat
p_reference as a reference. It is not a reference to the object, you
will get an error. That is why the prins in the 'or' example above.


Not at all.


You need to get a handle on the difference between a pointer to
something and the something and how they are represented in C++. Your
p_reference is a pointer (*p_reference) is a reference. A reference to
an object looks just like an object.