Access to private members using pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FNA access
    New Member
    • Jun 2007
    • 36

    Access to private members using pointers

    Hello all,

    I am somewhat new to C++, so please forgive my ignorance. I have an assignment that I am working on and while I do not want someone to do my homework for me, some advice to point me in the right direction would be helpful.

    I have a Class CProduct that contains two private members of type string( we will say stringA and stringB).

    I am supposed to create an array of CProduct objects and then create two arrays of pointers to point to the each CProduct within the array.

    I have been able to get this far.

    My next task is to then rearrange the pointers in each array so that one array is has the pointers in the alpabetical order according to the CProduct.string A it points to. The second pointer array needs to be reordered so that its pointers are in the alphabetical order according to the CProduct.string B it points to.

    The problem is that stringA and stringB are both private members of CProduct.

    Unfortunatley I have spent 3 days searching through books, and the net to find a solution, other than either making stringA and stringB public or using accessor functions.(niet her of which is an option for this assignment)

    As mentioned this is an assignment, but my prof has gone to India for 2 weeks mid semester, and this is due when she gets back. I am not looking for the complete answer, but some suggestions to get me going in the right direction

    I have included the code for my CProduct Class below:

    Code:
    #ifndef CPRODUCT_H
    #define CPRODUCT_H
    
    #include"MyString.h"
    #include <iostream>
    
    using namespace std;
    
    class CProduct
    {
    public:
    
    	CProduct();
    
    	CProduct(const MyString prodName_in, const MyString prodID_in, const int prodCategory_in, const double prodDetails_in[]);
    
    	CProduct(const CProduct &productRHS);
    
    	~CProduct();		
    
    	CProduct &operator = (const CProduct &productRHS);		
    
    	friend istream &operator >> (istream &inStream, CProduct &productRHS);
    
    	friend ostream &operator << (ostream &outStream, const CProduct &productRHS);
    
    	static int Count();
    
    private:
    
    	MyString prodName;
    	MyString prodID;
    	int prodCategory;	
    	double prodDetails[5];
    	
    	
    	void errorCheck();
    	
    	static int cnt;
    	static void DecProd();
    	static void IncProd();
    
    };
    #endif //CPRODCUT_H
    MyString is pretty much the same as string class, I had to build it from Cstring to use in this new application.

    Any insights would be greatly appreciated.

    Thank you in advance
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Do you want to access private members of a class using pointers?

    If yes then what you should do is typecast the class pointer to a char * pointer.
    You should know the position of the member then you can increment the char * pointer to access the same.

    Raghuram

    Comment

    Working...