casting pointers

Y

Ying Yang

Hi,

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

<snippet>

Node2* link2;
Node1* link1;

link1 = link2 //error


Regards
wewewewe
 
R

Ron Natalie

Ying Yang said:
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?
 
K

Kevin Goodsell

Ying said:
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
 
K

Kevin Doyle

A new C++ type REtard->Kevin Goodsell
Kevin Goodsell said:
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
 
K

Kevin Goodsell

Kevin said:
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.

-Kevin
 
H

Howard

Ron Natalie said:
Certainly is an error. What are you really trying to do?

How are Node1 and Node2 related?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
 
Y

Ying Yang

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
 
Y

Ying Yang

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.


wewewe
 
K

Karl Heinz Buchegger

Ying said:
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.
 
G

Gavin Deane

Ying Yang said:
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
 
R

Ron Natalie

Ying Yang said:
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.
 
Y

Ying Yang

Ying said:
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%, so does the above cast syntacally correct? or is the wrong
cast method used?


wewewewe
 
R

Ron Natalie

Ying Yang said:
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.
 
K

Karl Heinz Buchegger

Ying said:
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.
 
S

Sumit Rajan

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top