4 に答える
To initialize the fields of a class you use the initialization list syntax:
class Toy {
public:
std::auto_ptr<int> line;
Toy() : line(new int(3))
{
}
};
otherwise, you may get a default-initialized line
and reseat it with its reset
method:
class Toy {
public:
std::auto_ptr<int> line;
Toy()
{
line.reset(new int(3));
}
};
But there are more problems with this code; first of all, new int(3)
does not create an array of three int
s (as I think you think), but it creates a single int
initialized to 3
. What you probably meant was new int[3]
.
But: new int[3]
would need a delete[]
to be freed, but auto_ptr
uses plain delete
, i.e. it's not intended to manage arrays. This because the solution provided by the standard library to manage arrays is std::vector
, which you should probably use instead of your homebrew solution, since std::vector
has virtually no overhead over a "normal" dynamic array.
Your trouble isn't with auto_ptr
, but with constructors in C++. You need to initialize class members in the constructor initializer list:
Toy() : line(new int(3)) { }
Note that this creates a single dynamic int
with value 3
.
You are not calling a constructor. Instead, you try to call a non-existent function-call operator. These would call the constructor:
Toy() : line(new int(3)) {
or (not exactly what you want)
Toy() {
auto_ptr<int> line(new int(3));
or (as above, not what you want)
Toy() {
auto_ptr<int>(new int(3));
That's not where you construct member variables. You want to use an initializer list.
Toy()
: line(new int(3))
{ }
Members are constructed just before your classes constructor code runs. You can assign them in the constructor code though. They are constructed in the order they appear in the class definition, not in the order they appear in this list. Most compilers will warn you if this list is out of order, just because that avoids confusion. You can also construct parent objects in the same way, by using the parent class' type (since it has no name)
class Toy : public Thing {
Toy() : Thing("APPLE") {}
};