Thread: classes

  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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > 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
    Ireland
    Posts
    8,446
    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.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    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
    Ireland
    Posts
    8,446
    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;
    }
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    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.
    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
    Ireland
    Posts
    8,446
    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.
    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,895
    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
    Ireland
    Posts
    8,446
    The point was to illustrate the use of classes with all private members.
    The point is also to share use_count.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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
    Ireland
    Posts
    8,446
    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++
    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.

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, 09: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, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM