Solved

Casting from pointer to int

Posted on 2000-04-04
5
245 Views
Last Modified: 2010-04-02
This code works:
  if ((int) pNode()->pUserField() == 2) .....

but am not sure how to do the opposite ie. plug a value back into the object pNode.   I have tried

  pNode()->pUserField() = (void *) 2;

but this doesn't compile.  How do I set the pUserField to an integer value?

0
Comment
Question by:abulka
5 Comments
 

Expert Comment

by:ottenm
ID: 2686170
Short answer?  Use another function (not pUserField()).  i.e., pUserField() is a function that, if the second line you show doesn't compile, does not return something that can show up on the left hand side of an assignment operator (an "l-value").  Maybe you can rewrite the function pUserField() (and note, it is a function, not a variable, as the name would lead you to believe) to return by reference.  Then you'd be all set!
0
 

Author Comment

by:abulka
ID: 2686199
I am using a 3rd party library and cannot rewrite or add new functions.  It should be possible to do with the existing function.  Here is more info on the UserField property:



//
// This method sets a void pointer to a user field. This pointer is
// originally initialized to NULL.
//

void * TSDigraph::pUserField(void * pUserField);


//
// This method gets a void pointer to a user field. This pointer is
// originally initialized to NULL.
//

void * TSDigraph::pUserField();


0
 
LVL 3

Accepted Solution

by:
GlennDean earned 5 total points
ID: 2686239
Instead of writing
pNode()->pUserField() = (void *) 2;
write
pNode()->pUserField((void *)2);
   Glenn
0
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2686254
I work for Tom Sawyer and I can tell you that Glenn is definitely right. He fully deserves the enormous 5 point reward :)
0
 

Author Comment

by:abulka
ID: 2686327
Seems to work.  Much appreciated!  Thanks to mnewton, too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now