Constructor? or something else? [duplicate]
Possible Duplicate:
What is this weird colon-member syntax in the constructor?
So I was looking through some samples in a Direct X 10 book, when I came across this
PeaksAndValleys::PeaksAndValleys()
: mNumRows(0), mNumCols(0), mNumVertices(0), mNumFaces(0),
md3dDevice(0), mVB(0), mIB(0)
{
}
I understand this is probably supposed to be a constructor......but i've never seen开发者_如何学Python one like this? can someone explain? specifically why is there a single : after it's declared?
Constructor Initialization list
This is called an initialization list, and is used for initializing class members. It is useful both as a shorthand and it is required for initializing members that do not have a default constructor. For example:
#include <iostream>
using namespace std;
class Foo {
public:
Foo(int x) { cout << "Foo(" << x << ")" << endl; }
};
class Bar : public Foo {
Foo member;
public:
Bar() { /* error */ }
};
This gives an error because member
cannot be default constructed, as Foo
does not have a default constructor. Change it to
Bar(): member(42) {}
and now it works.
This syntax is also useful for initializing const
members of a class, as while they might be default-constructible, you cannot overwrite them in the constructor body.
class Baz {
const member;
public:
Baz(int x): member(x) {}
};
The same idea also applies to references, as they also must be initialized directly. Finally, it is used for specifying arguments for the base class constructor.
class Xyzzy : public Foo {
public:
Xyzzy(int y): Foo(y+3) {}
};
That's just a syntax for setting the class fields. In most cases, it's functionally identical to the version you're expecting where each field is set in the constructor body. It lets you pass arguments to constructors where you otherwise might not be able to. However, there are certain other circumstances where you need this syntax (assigning member references, etc.). You'll also sometimes see constructor chaining, where you call the base class constructor in the same way instead of just setting member variables.
class Base {
public:
Base(int n) {}
};
class Derived : public Base {
public:
Derived(int n) : Base(n) {}
};
It's called a constructor initializer.
ClassName::ClassName(Type someArgument) : memberName(initialValue),
otherMember(someArgument)
{
//constructor logic
}
It sets memberName
to initialValue
and otherMember
to someArgument
. Several members are separated by a ,
.
I just want to add:
You it whenever you can! It's a lot faster that using classical method. You can use it to initialize constants, members, call parent constructors, etc.
Everything you specify in this list, will be created before you create class instance!
The :
syntax allows you to specify arguments for the constructors of class members.
As an example, this class has a member named md3dDevice
whose constructor takes an integer (or maybe a pointer) as an argument. Using the :
operator you can set that constructor's argument.
精彩评论