I am totally confused. In this example which is part of my class, I have:
public:
typedef DoubleLinkedNode<DataType> Node;
private:
const DataType* fValue;
Node* fNext;
Node* fPrevious;
DoubleLinkedNode(): fValue((const DataType*)0){
fNext = (Node*)0;
fPrevious = (Node*)0;
}
Which means fValue
is a const DataType*
, now I want to apply/define data type like string
or int
to fValue
by this part:
开发者_JAVA技巧 DoubleLinkedNode( const DataType& aValue ){
std::cout << " -- " << aValue << std::endl;
}
I am confused , what exactly I must write there and why? How can I define aValue
to my fValue
?!
(note: The std::cout << " -- " << aValue << std::endl;
is just for test)
I'm not quite sure what you're trying to do here, but if you want to construct DoubleLinkedNode with fValue pointing to the address of aValue (which was passed to the constructor by reference) you need to define your constructor this way:
DoubleLinkedNode( const DataType& aValue ) : fValue(&aValue) {
std::cout << " -- " << aValue << std::endl;
}
Note that it's not 100% safe to do this, since you can accidentally call this constructor with an rvalue reference (to simplify things: a reference to an object that gets destroyed right after the function call). For instance, the following code will not raise a compilation error:
std::string s = "Hello ";
DoubleLinkedNode<std::string> node = DoubleLinkedNode<std::string>(s + "World");
even though s + "World"
is a temporary value which will be promptly destroyed after the constructor call, and now fValue will point to an invalid memory location. This is very bad, since you won't get any warning during compilation, but you will get some very hard-to-debug behavior at runtime.
Therefore, it might be better to make a constructor that expects pointers instead of references:
DoubleLinkedNode( const DataType* aValue ) : fValue(aValue) {
std::cout << " -- " << aValue << std::endl;
}
Since fValue
is a pointer, and DoubleLinkedNode() takes an object by reference, you need to dereference the pointer, like this:
DoubleLinkedNode(*fValue);
If fValue
is a pointer, it needs to point at some variable created elsewhere. So what code is responsible for the lifetime of the pointed-at value *fValue
?
If the class needs to create the value itself and fValue
really needs to be a pointer, it can use new
and delete
:
template <typename DataType>
DoubleLinkedNode<DataType>::DoubleLinkedNode( const DataType& aValue )
: fValue( new DataType(aValue) ), fNext(0), fPrevious(0)
{}
template <typename DataType>
DoubleLinkedNode<DataType>::~DoubleLinkedNode() {
delete fValue;
}
But I suspect the design might work out better if fValue
were not a pointer in the first place:
private:
const DataType fValue;
// Requires a default constructor for DataType.
template <typename DataType>
DoubleLinkedNode<DataType>::DoubleLinkedNode()
: fValue(), fNext(0), fPrevious(0)
{}
template <typename DataType>
DoubleLinkedNode<DataType>::DoubleLinkedNode( const DataType& aValue )
: fValue(aValue), fNext(0), fPrevious(0)
{}
精彩评论