classes

This is a discussion on classes within the C++ Programming forums, part of the General Programming Boards category; ? why are these so usefull if you took out the public keywords itd look like its not a class...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    classes

    ? why are these so usefull if you took out the public keywords itd look like its not a class

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Then it's a struct. And if classes are not useful you can email Bjarne and ask him to take them out in the next C++ standard
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    > Then it's a struct.
    It would be a class with all private members.

    You're correct, a class with all private members probably wont be much use, but that is only because you haven't developed a, interface yet, so it can't interact with the programmer or the user.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    Classes with all private members have their uses. Implementation of smart pointers to name one. And classes built to manage other classes to name another.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Classes with all private members have their uses. Implementation of smart pointers to name one. And classes built to manage other classes to name another.
    hmm... why would one implement a smart pointer with only private members? I suppose one could provide a public interface by means of friend functions, but that seems rather silly when things like operator++ could be a member function with public access.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Mario F.
    Classes with all private members have their uses. Implementation of smart pointers to name one. And classes built to manage other classes to name another.
    How would you access the pointer if all the access functions were private ?
    Kurt

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    Bellow, an example of a smart pointer implementation using a Use Count technique derived from Boost's shared_ptr.

    Sources: C++ Primer and Boost shared_ptr.

    The Use_count class has all private members. It manages the pointer in Ptr_Class (some class that has a pointer). Notice that Ptr_class data member pointer is declared of type Use_Count.

    Code:
    class Use_count {  // Smart pointer being implemented through a use count
        friend class Ptr_class;
    
        Use_count(int* p): ip(p), counter(1) {};
        ~Use_count() { delete ip; }
    
        int* ip;
        size_t counter;
    };
    
    class Ptr_class {  // Some class with a pointer member
    public:
        Ptr_class(int* p): ptr(new Use_count(p)) {}
        Ptr_class(const Ptr_class &obj):ptr(obj.ptr) { ++ptr->counter; }
        ~Ptr_class() { if(--ptr->counter == 0) delete ptr; }
    
        Ptr_class& operator=(const Ptr_class&);
        
        int* get_ptr() const { return ptr->ip; }
        void set_ptr(int* i) { ptr->ip = i; }
        int get_ptr_val() const { return *ptr->ip; }
        void set_ptr_val(int i) { *ptr->ip = i; }
    private:
        Use_count* ptr;
    };
    
    Ptr_class& Ptr_class::operator=(const Ptr_class &obj) {
        ++obj.ptr->counter;
        if(--ptr->counter == )
            delete ptr;
        ptr = obj.ptr;
        return *this;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Oh, but that is using a helper class that only has private members (your second point), not creating a smart pointer class with only private members. An alternative would be to nest Use_count in Ptr_class.

    EDIT:
    I guess "implementation of smart pointers" is still correct, but that's rather misleading in my opinion, since your next point makes it seem as if you mean to say that the actual smart pointer class is the one with no public members.
    Last edited by laserlight; 07-05-2006 at 09:12 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    The idea of not implementing the use count inside the Ptr_class is to make this a shared smart pointer class. Other classes implementing data member pointers of the same type can reuse Use_Count. At least that was my understanding of this type of smart pointer implementation.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    Quote Originally Posted by laserlight
    I guess "implementation of smart pointers" is still correct, but that's rather misleading in my opinion, since your next point makes it seem as if you mean to say that the actual smart pointer class is the one with no public members.
    You are correct. I'm not applying the term correctly. The smart pointer gets implemented through Use_Count.

    EDIT: Or better yet, ptr in Ptr_Class is the smart pointer here. It gets implemented through Use_Count.
    Last edited by Mario F.; 07-05-2006 at 09:31 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I think that's poor design. It would be better to make the class Use_count itself a private member of Ptr_class.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    The point was to illustrate the use of classes with all private members.
    The point is also to share use_count.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    The point was to illustrate the use of classes with all private members.
    The point is also to share use_count.
    The way I see it, Use_count could be nested in a base class (perhaps as a protected member), then the child classes would also 'share' it. This may offer more flexibility since one would not have to modify Use_count to allow more classes to 'share' it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Mario F.
    The point was to illustrate the use of classes with all private members.
    The point is also to share use_count.
    If the only use case of a feature is bad design, is it still a valid argument for the feature?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,472
    I've just learned it is indeed. They even say it's in the eye of the beholder.

    But hey, let's all pretend and agree that there is no place for classes with all private members in C++
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21