? why are these so usefull if you took out the public keywords itd look like its not a class
Printable View
? 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 :)
> 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.
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:
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 ?Quote:
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;
}
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.
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.
You are correct. I'm not applying the term correctly. The smart pointer gets implemented through Use_Count.Quote:
Originally Posted by laserlight
EDIT: Or better yet, ptr in Ptr_Class is the smart pointer here. It gets implemented through Use_Count.
I think that's poor design. It would be better to make the class Use_count itself a private member of Ptr_class.
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:
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?Quote:
Originally Posted by Mario F.
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++
I actually agree with you in the other thread. The do...while(0) construct is nice in C when function-style macros are used, but in C++ you really shouldn't use it.
I still maintain that a class with all private members that just friends other classes (if it doesn't friend anything, it's useless anyway) should be converted as laserlight suggested.
I'm not sure if I understood how to implement it. But will surely give it a look. The issue of classes is still something I'm studying.
Like this:
Code:class ptr_class_base
{
protected:
class use_count
{
public:
// members here
};
};
class ptr_class : private ptr_class_base
{
// use_count is visible and accessible here
};
// Now we suddenly need another class that needs to use use_count.
class another_ptr_class : private ptr_class_base
{
// use_count is visible here - and we didn't have to change it
};