Thread: Accessing data declared private within header file

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    7

    Accessing data declared private within header file

    I have a header file that declares some fields as private, I then have a class that I need to compare two of the objects' information for equality but neither of them are the calling objects. I cannot alter the header file. How would I go about comparing private data fields? I will enter a brief bit of code for clarity.
    Thank You.

    Code:
    // Header File
    // stuff.h
    
    class stuff
    {
    private: 
      int* arr[20];
      int size;
    };
    bool equal (const stuff& a, const stuff& b);
    
    
    
    // Class File
    // stuff.cpp
    
    #include "stuff.h"
      
    stuff::stuff() 
    {
      size = 0;
    }
    
    bool equal  (const stuff& a, const stuff& b)
    {
    /* 
    Where I cannot figure out how to compare the two objects for equality due to private fields in the header 
    */
    }
    Last edited by googol; 02-10-2015 at 05:25 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    A guess from a C Programmer is that, you need to use the stuff::equal method.

    FYI: If you are trying to write the stuff::equal method you forgot the "stuff::".

    Edit: The above assumes a stuff::equal method exists; if it does NOT then I see little to do that does NOT violate the rules of good programming.

    Tim S.
    Last edited by stahta01; 02-10-2015 at 08:09 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The right approach is to either declare equal as a friend, or provide a public interface to access the member variables that are relevant for the comparison. If there is none, and you cannot change the class definition, then you should either accept that objects of the class were not designed to be compared for equality, or petition the class designer for the change, e.g., by submitting a bug report or feature request. You should not attempt to subvert private member access.

    EDIT:
    I'm assuming that you declared your own equal function and cannot figure out how to implement it. Of course, if such a function were already provided, use it!
    Last edited by laserlight; 02-10-2015 at 08:09 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    One questions that pops up is if you simply cannot use the implicit operator == for your stuff? It would compare all private data, not just bits, but maybe it's enough?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Elysia View Post
    One questions that pops up is if you simply cannot use the implicit operator == for your stuff? It would compare all private data, not just bits, but maybe it's enough?
    Another item of C++ knowledge that is new to me.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    One questions that pops up is if you simply cannot use the implicit operator == for your stuff?
    O_o

    What are you talking about?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by phantomotap View Post
    What are you talking about?
    She's probably talking about this. I didn't know it either, isn't this forum awesome? You get to learn new stuff!
    Devoted my life to programming...

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by GReaper View Post
    She's probably talking about this. I didn't know it either, isn't this forum awesome? You get to learn new stuff!
    That's even worse if he's confusing equality with assignment or whatever. I've always had to explicitly overload == and I don't think C++11 changes that. You don't get comparisons by default. You can use rel_ops and make comparisons a lot less work though!

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    It's a little confusing, but I think this new mechanism provides many different default methods. Of course, I'm not sure, since the details are a little sketchy. There's not enough info on the net yet.

    EDIT: Oh well, I don't know... Maybe I'm wrong.
    Last edited by GReaper; 02-11-2015 at 08:40 PM.
    Devoted my life to programming...

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    She's probably talking about this. I didn't know it either, isn't this forum awesome? You get to learn new stuff!
    O_o

    I doubt she was talking about those `default' functions. As whiteflags explains, the assignment, `operator = ', functions are not the same as the equality, `operator ==', functions. I admit that I haven't bought the C++14 standard, but the draft standard I have doesn't include any of the several proposals which would extend the `default' (explicitly-declared or implicit) mechanism to the equality operators.

    Soma
    Last edited by phantomotap; 02-11-2015 at 08:44 PM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It's a little confusing, but I think this new mechanism provides many different default methods. Of course, I'm not sure, since the details are a little sketchy. There's not enough info on the net yet.
    O_o

    The defaulted assignment operator, various constructors, and the destructor are C++11 standard. They have been available in several compilers for some time. You should be able to find ready information on the internet or books from the likes of Sutter or Meyers.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm talking out of my ass >_<
    Ignore what I just said. For some reason, I thought there was an implicit comparison operator.
    (Besides, it probably wouldn't make sense, as I just glanced over the OP again, in which the class uses pointers. I didn't look back at it when I wrote that, so I figured it didn't use pointers.)
    (PS: Yeah, you can remove that "like" )
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 04-05-2014, 09:23 PM
  2. Accessing private Data members
    By Emeighty in forum C++ Programming
    Replies: 17
    Last Post: 08-14-2008, 11:16 PM
  3. Question with accessing private data
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2008, 03:14 AM
  4. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  5. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM

Tags for this Thread