Solved

void pointer point to the class member?

Posted on 2000-04-05
12
270 Views
Last Modified: 2010-04-02
I define a void pointer p, a class A
I want p can point to the member in the A. How can I do it?
class A
{
public:
 int i1;
 char c1;
}
void A::*p[2];   //wrong when complie. :(
I hope can do this:
p[0]=&A::i1;
p[1]=&A::c1;
0
Comment
Question by:kanyuchun
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 5

Accepted Solution

by:
Jan Louwerens earned 200 total points
Comment Utility
you just have to do each one seperately. There is no automatic way to do it.

A aObj;
void* p[2];

p[0] = &(aObj.i1);
p[1] = &(aObj.c1);

you might need to cast the pointers to void to satisfy the compiler:

p[0] = (void*)&(aObj.i1);
0
 
LVL 3

Expert Comment

by:GlennDean
Comment Utility
jlouwere is right.  The only thing is i1 and c1 have to be public members.
   Glenn
0
 
LVL 3

Expert Comment

by:GlennDean
Comment Utility
Oops, forgot to mention that note jlouwere defined an actual object of class A.  
0
 
LVL 5

Expert Comment

by:Jan Louwerens
Comment Utility
yes, true, it can't be defined that way in a general case. The only way you could have it accessing class members without instantiating an object of that class is if those members are declared static.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
A pointer to member pointer cannot safely be cast to type void and vice versa.  This is because a pointer-to-member is not necessarily a true pointer.

Now if you create an instance of A, you can make a void pointer that points to a data member in that instance of A, but that is not a pointer member.
0
 

Expert Comment

by:bsimmons
Comment Utility
My comments will be in the /**/ style so that you can get a line-by-line idea of what you did wrong.

class A
{
public:
 int i1;
 char c1;
}

/*
Here you have said to the compiler "I want declare an array of 2 void pointers that is scoped inside of the A class."
No wonder the compiler screamed at you.
You can't use the scope operator (meaning the ::) unless you are referring to something that is already inside the class declaration.  You declared two variables: i1 and c1.
You can ONLY say A::i1 or A::c1.
*/
void A::*p[2];   //wrong when complie. :(

/* Here you have said, "Set the memory at index 0 in array p equal to the address of i1 which is scoped inside of the class A."
This too is wrong because you must first declare an instance of the a class.
*/
p[0]=&A::i1;
p[1]=&A::c1;


Here is how I would have written it given your class declaration:

   //Declare an instance of class A
   A Object;

   //Declare an array of void pointers
   void * p[2];

   //Set both indicies using class
   //notation
   p[0] = &Ojbect.i1;
   p[1] = &Object.c1;

That's it.

Good luck!!

Bryan
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:kanyuchun
Comment Utility
I have known about the void pointer already.
~(@_@)~
But I do not know how to accept both bsimmons' and jlouwere' answers. :-)
I satisfactory both your answer.
oh,what can I do? ;-)
How can I give bsimmons 100 points and give jlouwere 100 points ?
0
 
LVL 3

Expert Comment

by:GlennDean
Comment Utility
To me it is clear jlouwere gave the correct answer first (bsimmons basically rephrased jlouwere's answer).  You should of accepted his/her answer since it was correct.
  Glenn
0
 

Author Comment

by:kanyuchun
Comment Utility
ok ,I receive jlouwere' answer.
bsimmons,thank you too. :-)
GleenDean,thank you too. :-)

Thank all !!!

~(@_@)~

0
 

Author Comment

by:kanyuchun
Comment Utility
thank you very much!
0
 

Expert Comment

by:bsimmons
Comment Utility
My pleasure.  I was just trying to provide some detail.
0
 

Author Comment

by:kanyuchun
Comment Utility
hi,bsimmons:

thank you for your explain which help me to know more about C.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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

11 Experts available now in Live!

Get 1:1 Help Now