Solved

void pointer point to the class member?

Posted on 2000-04-05
12
277 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
ID: 2688357
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
ID: 2688418
jlouwere is right.  The only thing is i1 and c1 have to be public members.
   Glenn
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 2688425
Oops, forgot to mention that note jlouwere defined an actual object of class A.  
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2688434
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
ID: 2688498
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
ID: 2688542
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
 

Author Comment

by:kanyuchun
ID: 2688591
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
ID: 2688703
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
ID: 2688719
ok ,I receive jlouwere' answer.
bsimmons,thank you too. :-)
GleenDean,thank you too. :-)

Thank all !!!

~(@_@)~

0
 

Author Comment

by:kanyuchun
ID: 2688727
thank you very much!
0
 

Expert Comment

by:bsimmons
ID: 2689508
My pleasure.  I was just trying to provide some detail.
0
 

Author Comment

by:kanyuchun
ID: 2689551
hi,bsimmons:

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

838 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