473,385 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.stringA it points to. The second pointer array needs to be reordered so that its pointers are in the alphabetical order according to the CProduct.stringB 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.(niether 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:

Expand|Select|Wrap|Line Numbers
  1. #ifndef CPRODUCT_H
  2. #define CPRODUCT_H
  3.  
  4. #include"MyString.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. class CProduct
  10. {
  11. public:
  12.  
  13.     CProduct();
  14.  
  15.     CProduct(const MyString prodName_in, const MyString prodID_in, const int prodCategory_in, const double prodDetails_in[]);
  16.  
  17.     CProduct(const CProduct &productRHS);
  18.  
  19.     ~CProduct();        
  20.  
  21.     CProduct &operator = (const CProduct &productRHS);        
  22.  
  23.     friend istream &operator >> (istream &inStream, CProduct &productRHS);
  24.  
  25.     friend ostream &operator << (ostream &outStream, const CProduct &productRHS);
  26.  
  27.     static int Count();
  28.  
  29. private:
  30.  
  31.     MyString prodName;
  32.     MyString prodID;
  33.     int prodCategory;    
  34.     double prodDetails[5];
  35.  
  36.  
  37.     void errorCheck();
  38.  
  39.     static int cnt;
  40.     static void DecProd();
  41.     static void IncProd();
  42.  
  43. };
  44. #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
Mar 15 '08 #1
1 2829
gpraghuram
1,275 Expert 1GB
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
Mar 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: Chua Wen Ching | last post by:
Hi there, I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up! Which one is correct? Scenario: WForm.cs - the one that calls...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
0
shouvik
by: shouvik | last post by:
Say if we ve certain private methods along with private data members and public methods. other than friend function is thr any other way we access these members(they being private only) directly....
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.