template <class T>
class SmartPtr
{
public:
explicit SmartPtr(T* pointee) : pointee_(pointee);
SmartPtr& operator=(const SmartPtr& other);
~SmartPtr();
T& operator*() const
{
...
return *pointee_;
}
T* operator->() const
{
...
return pointee_;
}
private:
T* pointee_;
...
};
i can see how the rest of this works but than line resembles inheritance, am i right?
regards
ace
What does this line of C do?
Its from a smart pointer class definition:
explicit SmartPtr(T* pointee) : pointee_(pointee);
It has nothing to do with inheritance. It''s the initializer
list that, well, initializes the member variables.
Kami no Itte ga ore ni zettai naru!
list that, well, initializes the member variables.
Kami no Itte ga ore ni zettai naru!
It''s also C++, not C. They are not the same language, despite a great many similarities in syntax and behaviour.
The C++ FAQ Lite is full of useful information, such as this.
The C++ FAQ Lite is full of useful information, such as this.
A simpler example would be this random class I made:
class CExample{ public: CExample() : mA(0), mB(0.0), mC(''\0'') { } CExample(int a, double b, char c) : mA(a), mB(b), mC(c) { } private: int mA; double mB; char mC;};
This is basically (except for some performance technicalities in certain situations) identical to: class CExample{ public: CExample() { mA = 0; mB = 0.0; mC = ''\0''; } CExample(int a, double b, char c) { mA = a; mB = b; mC = c; } private: int mA; double mB; char mC;};
The benefit of the initializer list is that things get assigned a value only once. If one of your member variables is some complicated class, and assigning a value to that class takes a lot of effort on the computer, then initializing it in the initializer list will simply call the constructor which takes a value (or values) to construct the member variable with. If you use the second form, however, the default constructor of your member variable gets called, and then the assignment gets called when you actually set it to a specific value. This could possibly be incredibly inefficient. In the case of basic data types, I don''t think there is any performance difference, though. Just a matter of style.
SO if i want to use the smart pointer class i put at teh top, how would i create a new object. Im following a tutorial an dit doesnt work, giving me one of those annoying liner errors?
regards
ace
regards
ace
Say you have an int* p. Then you create a SmartPtr to the same address using SmartPtr s = (SmartPtr) p.
... when I read that I figured out easily enough that it was a ctor with initializer list; but what is the keyword "explicit"? o_O
A normal constructor that takes a single argument is also an implicit conversion; for instance, if your class A has a single-parameter int constructor, it is possible to implicitly convert (cast) an int to an A. If you wish to prevent this, to avoid mishaps, you may declare the constructor explicit so that you cannot construct an A without explicitly calling its constructor.
I''m extremely tired and have a feeling the above doesn''t make much sense, but a kernel of truth is still present.
I''m extremely tired and have a feeling the above doesn''t make much sense, but a kernel of truth is still present.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement