473,569 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access to private members using pointers

36 New Member
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:

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 2840
gpraghuram
1,275 Recognized Expert Top Contributor
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
3938
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 private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
12
2659
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 ) {cout<<"a.value="<<a.value<<endl; } private: int value;
6
4726
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 appreciated. Thanks in advance
3
1892
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 FileA.cs to access the structures FileA.cs - contains all structures
10
25721
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 ) Is there a way to get this information ? Thnaks in advance
5
2667
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 that's come before. To take an extreme example, suppose you have been writing Visual Basic applications for years now. If you're like many developers,...
23
2572
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. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of...
0
1183
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. its one of the requirements in a project of mine. The real prob comes when accessing function. say we've classes ported to us in .obj format. then...
7
2177
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.