? why are these so usefull if you took out the public keywords itd look like its not a class
? why are these so usefull if you took out the public keywords itd look like its not a class
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;}
> 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.
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.
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.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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
How would you access the pointer if all the access functions were private ?Originally Posted by Mario F.
Kurt
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.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
You are correct. I'm not applying the term correctly. The smart pointer gets implemented through Use_Count.Originally Posted by laserlight
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.
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
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.
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.The point was to illustrate the use of classes with all private members.
The point is also to share use_count.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If the only use case of a feature is bad design, is it still a valid argument for the feature?Originally Posted by Mario F.
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
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.