How should I interpret this struct in c++?
class new_hashtable {
public:
/* This is the datatype stored in the hashtable slots. */
struct KEY_VALUE {
KEY k;
VALUE v;
KEY_VALUE(const KEY &k_,const VALUE &v_) :k(k_), v(v_) {}
KEY_VALUE() {}
};
}
In above codes, is KEY_VALUE(const....开发者_如何学C) :k(k_), v(v_){}
a copy constructor?? What does k(k_), v(v_)
part do ?
The difference between class
and struct
in C++ is that the default for struct
is public. Other than that they're exactly the same.
So you have an inner class KEY_VALUE
with all the members and methods public, with 2 constructors: the default, and one that receives 2 parameters. It is not a copy constructor. k(k_), v(v_)
is the initialization list.
It is a constructor, not a copy constructor. A copy constructors would be: KEY_VALUE(const KEY_VALUE&)
.
Copy constructors are defined in §12.8
of the standard: A non-template constructor is a constructor where the first argument is X&
, const X&
, volatile X&
or const volatile X&
and all other, if any, arguments have default values.
Everything after :
is called an initializer list
.
It is a regular constructor taking references to elements of type KEY and of type VALUE. k(k_) and v(v_) automatically initialize KEY k and VALUE v with k_ and v_ as per the constructors defined in KEY and VALUE types, respectively.
KEY_VALUE(const KEY &k_,const VALUE &v_)
is not a copty constructor - it's a regular old constructor.
The :k(k_), v(v_)
is an initialize list that initializes the k
and v
members of the struct.
Remember that in C++ a struct is the same thing as a class except that the default access to members is public instead of private, and if it inherits form another class, the default is again public inheritance instead of private.
A copy constructor would have to have one of the following signatures:
KEY_VALUE(const KEY_VALUE&);
KEY_VALUE(KEY_VALUE&);
A copy constructor would be coded like this:
KEY_VALUE( const KEY_VALUE& iK )
: k( iK.k )
, v( ik.v )
{}
The line
KEY_VALUE( const KEY& k_, const VALUE& v_ )
: k( k_ )
, v( v_ )
{}
is simply a constructor parametrized on KEY and VALUE.
That is not a copy constructor, just a constructor with two parameters... in C++ the syntax:
KEY_VALUE(const KEY &k_,const VALUE &v_) :k(k_), v(v_) {}
is more or less equivalent to
KEY_VALUE(const KEY &k_, const VALUE &v_)
{
k = k_;
v = v_;
}
Note that this is not 100% true as in the second form k
and v
are first initialized by the default constructor and later they are assigned.
Note also that it's also common to name the parameter exactly as the member, thing that makes the syntax even fancier...
KEY_VALUE(const KEY &k,const VALUE &v) :k(k), v(v) {}
If you don't know the initialization list syntax then I assume you never read any decent C++ book. Please do yourself a favor and stop just experimenting C++ with a compiler... C++ cannot be learned that way, it must be studied.
Pick a good book like TCPPPL and read it from cover to cover... even if you are smart there is no way you can learn C++ by experimentation (actually the smarter you are and the harder it will be: in quite a few places C++ is NOT a logical language and there's no way you can guess what a committee decided because of political or historical reasons).
精彩评论