Thread: different ways of declaring classes

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    different ways of declaring classes

    I have a class, within which I have the need for several different data structures. I have no use for these structures outside of the class, so I've been declaring them like this:

    Code:
    //an obviously simplified version
    class myClass
    {
         struct dataStructure
         {
              int data;
              char letter;
         }
    
         dataStructure foo;
    }
    A few questions
    1) this does prevent dataStructure from being used outside of myClass, correct?
    2) are there any performance problems with doing it this way?
    3) ...other concerns about doing it this way?
    Away.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    1) yes and no. in your code yes because the outer class is a class and as such the inner struct is private to it as there is no access specifier. had it been in the public section then it could have been used like so...
    outer_class::inner_class myobj;

    2)no. its good usual pseudo-data hiding practice.

    3) none but look into the pimpl idiom and the bridge design pattern. Good info on pimpl idiom here
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Stoned_coder, are you sure you posted the right link? I searched for both "pimpl" and "bridge" on that site, and found neither. Thanks for the speedy response on the question, though.
    Away.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    start at number 1 and work thru. u will learn more than u can imagine. There is plenty of info on pimpl idiom there. search for compilation firewall. For bridge pattern try google. visit pattern repository. good articles also on codeproject.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    look at 24 and 28 for starters
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    7

    hehe

    private member will prevent dataStructure from being used outside of yourClass
    Last edited by ck_chuyun; 08-18-2003 at 01:27 AM.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I read about the pimple idiom. From what I gathered it is just a way to hide the details of a class implementation to improve compile times and (dependencies?). Is this correct. Very nearly over my head type of stuff. Ohh one question suppose you have a pointer that points to the class with the members you are hiding what then is the syntax for accessing these members. Also why is it called the pimple idiom?

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In the name, 'p' stands for pointer, and 'impl' for implementation - comes out to pimpl. Someone just wanted something pronounceable (though personally, I usually use the name 'data_ptr' instead of 'pimpl').

    The implementation structure should be a private member. Basically, it is a collection of all (or most) of your member data, and private utility functions (with some exceptions). Nothing outside the class should be able to access it, only the class implementation itself. Even derived classes (in most casses) should not have access to this information. To operate on the class, you just use the provided interface.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  2. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 06:49 AM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM