Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

void pointer point to the class member?

Posted on 2000-04-05
12
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 5

Accepted Solution

by:
Jan Louwerens earned 600 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.
Suggested Courses

715 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