Exforsys
+ Reply to Thread
Results 1 to 3 of 3

Class and Pointers

This is a discussion on Class and Pointers within the C and C++ forums, part of the Programming Talk category; I have my class defined as sample. I want to declare a pointer to an int member of class sample ...

  1. #1
    sammy is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    145

    Class and Pointers

    I have my class defined as sample. I want to declare a pointer to an int member of class sample and also initialize the pointer to a member. Kindly tell me how I can achieve this. I know concepts of pointers and class separately but want to try out combining both in this example. Kindly help me out.


  2. #2
    hvg_99 is offline Junior Member Array
    Join Date
    May 2007
    Answers
    9

    Pointers

    class sample{
    public:
    int i;
    }

    int main(){
    // declare a pointer to a data member
    int sample::*ptr=&sample::i;
    }

    Here 'i' is a member of sample class and ptr is a pointer to a member of sample class.

    I hope i have cleared your doubt,

    Regards,
    Gayatri


  3. #3
    sammy is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    145
    It was a excellent explanation that clarified my doubt about declare a pointer to member of a class and also on initializing the pointer to the member. Kindly tell me whether we can handle pointers concepts in the same way as we handle in C or is there any restriction in C++ or any new feature related to pointer usage in C++ which is not in C.


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...