Archived

This topic is now archived and is closed to further replies.

dave

What does this line of C do?

Recommended Posts

Its from a smart pointer class definition: explicit SmartPtr(T* pointee) : pointee_(pointee);
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Say you have an int* p. Then you create a SmartPtr to the same address using SmartPtr s = (SmartPtr) p.

Share this post


Link to post
Share on other sites
... 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites