Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

casting pointers

瀏覽次數:0 次
跳到第一則未讀訊息

Ying Yang

未讀,
2003年9月8日 上午11:35:342003/9/8
收件者:
Hi,

How to I make pointers of different object types point to each other?

<snippet>

Node2* link2;
Node1* link1;

link1 = link2 //error


Regards
wewewewe


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003


Ron Natalie

未讀,
2003年9月8日 上午11:39:242003/9/8
收件者:

"Ying Yang" <Ying...@hotmail.com> wrote in message news:3f5ca...@news.iprimus.com.au...

> Hi,
>
> How to I make pointers of different object types point to each other?
>
> <snippet>
>
> Node2* link2;
> Node1* link1;
>
> link1 = link2 //error

Certainly is an error. What are you really trying to do? How are Node1 and Node2 related?


Kevin Goodsell

未讀,
2003年9月8日 下午5:07:302003/9/8
收件者:
Ying Yang wrote:

> Hi,
>
> How to I make pointers of different object types point to each other?

You don't. The language prevents this. It can be overcome by using
casts, but this is dangerous and the result may not work at all.

You are attempting to do things that far exceed your knowledge of the
language. I suggest you go back and learn the language first.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Kevin Doyle

未讀,
2003年9月8日 下午5:33:312003/9/8
收件者:
A new C++ type REtard->Kevin Goodsell
"Kevin Goodsell" <usenet1.spa...@neverbox.com> wrote in message
news:mc67b.3766$Yt....@newsread4.news.pas.earthlink.net...

Kevin Goodsell

未讀,
2003年9月8日 下午5:48:112003/9/8
收件者:
Kevin Doyle wrote:

> A new C++ type REtard->Kevin Goodsell

Your inability to grasp a simple concept such as NOT TOP-POSTING
strongly suggests that the mental deficiency is yours, not mine.

If you can find any error in my post you are more than welcome to point
it out. If not, then your abusive post would seem to be completely
unwarranted.

Howard

未讀,
2003年9月8日 下午6:38:352003/9/8
收件者:

"Ron Natalie" <r...@sensor.com> wrote in message
news:3f5ca2ae$0$46507$9a6e...@news.newshosting.com...

Yeah, that's certainly what needs answering here. My *guess* is the OP is
using a derived class (Node2) and trying to pass around base-class (Node1)
pointers. If not, then it's definitely a "bad thing". But, even that *can*
be accomplished , by using void* pointers. Not a good idea though, unless
you *really* know what you're doing and why.

-Howard

Default User

未讀,
2003年9月8日 下午6:42:082003/9/8
收件者:
Kevin Doyle wrote:
>
> A new C++ type REtard->Kevin Goodsell


*pa-lonk*

Brian Rodenborn

Ying Yang

未讀,
2003年9月9日 清晨5:39:392003/9/9
收件者:

> Kevin Doyle wrote:
>
> > A new C++ type REtard->Kevin Goodsell
>
> Your inability to grasp a simple concept such as NOT TOP-POSTING
> strongly suggests that the mental deficiency is yours, not mine.

I hear you - how strange.

> If you can find any error in my post you are more than welcome to point
> it out. If not, then your abusive post would seem to be completely
> unwarranted.

wewewe

Ying Yang

未讀,
2003年9月9日 清晨5:41:552003/9/9
收件者:

> Ying Yang wrote:
>
> > Hi,
> >
> > How to I make pointers of different object types point to each other?
>
> You don't. The language prevents this. It can be overcome by using
> casts, but this is dangerous and the result may not work at all.

How would I go about it using casts?

link1 = (*link1)link2; //right?


> You are attempting to do things that far exceed your knowledge of the
> language. I suggest you go back and learn the language first.

Karl Heinz Buchegger

未讀,
2003年9月9日 上午8:23:152003/9/9
收件者:

Ying Yang wrote:
>
> > Ying Yang wrote:
> >
> > > Hi,
> > >
> > > How to I make pointers of different object types point to each other?
> >
> > You don't. The language prevents this. It can be overcome by using
> > casts, but this is dangerous and the result may not work at all.
>
> How would I go about it using casts?
>
> link1 = (*link1)link2; //right?

You need to be aware what the above effectively does:
It tells the compiler:

'Dear Compiler. Shut up!
I am the programmer and you better do what I tell you to do.
I don't care that you think that those types don't fit, I don't even
want to know. I am the programmer and I have the power to do this,
for the simple reason that I know what I do. So once again:
Shut up before I shut the computer down!'

Needless to say that the programmer in 95% of all cases does *not*
know what he/she is doing.

--
Karl Heinz Buchegger
kbuc...@gascad.at

Gavin Deane

未讀,
2003年9月9日 上午9:02:482003/9/9
收件者:
"Ying Yang" <Ying...@hotmail.com> wrote in message news:<3f5da...@news.iprimus.com.au>...

> > Ying Yang wrote:
> >
> > > Hi,
> > >
> > > How to I make pointers of different object types point to each other?
> >
> > You don't. The language prevents this. It can be overcome by using
> > casts, but this is dangerous and the result may not work at all.
>
> How would I go about it using casts?
>
> link1 = (*link1)link2; //right?

Wrong. That doesn't even compile.

Your compiler is complaining because it really doesn't want to assign
link2 to link1. It thinks that's a dangerous or incorrect thing to do.
You could "shut the compiler up" with a correct cast. But the compiler
is your friend, you should only overrule it if you know exactly what
you are doing and why. Since you are not even sure of the syntax for
casting, it seems unlikely that you can carefully judge that casting
really is appropriate in this case.

That is why people have asked you for more information about the
relationship between the Node1 and Node2 types, and an explanation of
what you are trying to do. They want to help you avoid further
problems a short way down the line. Nobody wants to show you how to do
the cast and leave it at that when it seems very likely that casting
will just cause more problems.

So, how are the Node1 and Node2 types related (if they are related at
all), and why are you trying to assign a Node2 to a Node1?

GJD

Ron Natalie

未讀,
2003年9月9日 上午9:21:262003/9/9
收件者:

"Ying Yang" <Ying...@hotmail.com> wrote in message news:3f5da...@news.iprimus.com.au...

>


> How would I go about it using casts?
>
> link1 = (*link1)link2; //right?
>

Wrong.

We can't tell you how to "go about it" until you tell us what
it is that you are trying to do. Why do you want to assign
these pointers? What are link1 and link2? We can't
answer this on such little information.


Ying Yang

未讀,
2003年9月9日 上午9:37:252003/9/9
收件者:


I'm the other 5%, so does the above cast syntacally correct? or is the wrong
cast method used?


wewewewe

Ron Natalie

未讀,
2003年9月9日 上午10:18:412003/9/9
收件者:

"Ying Yang" <Ying...@hotmail.com> wrote in message news:3f5dd...@news.iprimus.com.au...

>
> I'm the other 5%, so does the above cast syntacally correct? or is the wrong
> cast method used?

What you wrote is never correct. We can't tell you what is correct if you don't
tell us what it is you want to happen. We and your compiler are not clairvoyant.
You must specify exactly what you want done.


Karl Heinz Buchegger

未讀,
2003年9月9日 上午10:27:282003/9/9
收件者:

Ying Yang wrote:
>
> > Ying Yang wrote:
> > >
> > > > Ying Yang wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > How to I make pointers of different object types point to each
> other?
> > > >
> > > > You don't. The language prevents this. It can be overcome by using
> > > > casts, but this is dangerous and the result may not work at all.
> > >
> > > How would I go about it using casts?
> > >
> > > link1 = (*link1)link2; //right?
> >
> > You need to be aware what the above effectively does:
> > It tells the compiler:
> >
> > 'Dear Compiler. Shut up!
> > I am the programmer and you better do what I tell you to do.
> > I don't care that you think that those types don't fit, I don't even
> > want to know. I am the programmer and I have the power to do this,
> > for the simple reason that I know what I do. So once again:
> > Shut up before I shut the computer down!'
> >
> > Needless to say that the programmer in 95% of all cases does *not*
> > know what he/she is doing.
>
> I'm the other 5%,

Honestly
What I have seen so far in this newsgroup from you: No, you are not.

> so does the above cast syntacally correct?

No.

> or is the wrong
> cast method used?

It is a C-style cast and there is only one syntax for it:

link1 = (Node1*)link2;

But as others have said already: Don't do it!

You behave like somebody wanting to know how to walk on a steel rope
high above a river. When asked why, it turns out that you want to cross
that river but don't know about the bridge 100 meter downstream. If
you tell us what you want to do, some of us will show you the bridge.

Sumit Rajan

未讀,
2003年9月9日 下午1:14:192003/9/9
收件者:
Karl Heinz Buchegger wrote:


> You behave like somebody wanting to know how to walk on a steel rope
> high above a river. When asked why, it turns out that you want to cross
> that river but don't know about the bridge 100 meter downstream. If
> you tell us what you want to do, some of us will show you the bridge.
>


Nice analogy, Karl!

0 則新訊息