Solved

void pointer point to the class member?

Posted on 2000-04-05
12
290 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 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
Independent Software Vendors: 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

626 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