Solved

void pointer point to the class member?

Posted on 2000-04-05
12
271 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
 
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
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.

 

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

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

920 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

13 Experts available now in Live!

Get 1:1 Help Now