最近boostを勉強しはじめたんですが、shared_ptrに入っているオブジェクトのポインタを ダウンキャストしたい時はどうすればいいんでしょうか? (例) class foo{}; class bar : public foo{}; typedef boost::shared_ptr<foo> pfoo;
foo* b1 = new bar(); pfoo b2 = pfoo(new bar());
// b1なら、 if (dynamic_cast<bar>(*b1)) cout << "this is bar1" << endl;
§5.3.4.15 15 A new-expression that creates an object of type T initializes that object as follows: . If the new-initializer is omitted: . If T is a (possibly cv-qualified) non-POD class type (or array thereof), the object is defaultinitialized (8.5). If T is a const-qualified type, the underlying class type shall have a user-declared default constructor. . Otherwise, the object created has indeterminate value. If T is a const-qualified type, or a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of const-qualified type, the program is ill-formed;
. If the new-initializer is of the form (), the item is value-initialized (8.5); . If the new-initializer is of the form (expression-list) and T is a class type, the appropriate constructor is called, using expression-list as the arguments (8.5); . If the new-initializer is of the form (expression-list) and T is an arithmetic, enumeration, pointer, or pointer-to-member type and expression-list comprises exactly one expression, then the object is initialized to the (possibly converted) value of the expression (8.5); . Otherwise the new-expression is ill-formed.
で、§8.5を見てみると・・・・
8 [Note: since () is not permitted by the syntax for initializer, X a(); is not the declaration of an object of class X, but the declaration of a function taking no argument and returning an X. The form () is permitted in certain other initialization contexts (5.3.4, 5.2.3, 12.6.2). ]
5 To zero-initialize an object of type T means: . if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T; . if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized; . if T is a union type, the object’s first named data member89) is zero-initialized; . if T is an array type, each element is zero-initialized; . if T is a reference type, no initialization is performed. To default-initialize an object of type T means: . if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); . if T is an array type, each element is default-initialized; . otherwise, the object is zero-initialized. To value-initialize an object of type T means: . if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); . if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
>>864 > pa2->print(); // もはや0である保証はない ↑ここも必ず 0 じゃね? new B() は "value-initialized" で B は "a class type with a user-declared constructor" じゃなくて "a non-union class type without a user-declared constructor" だから pa2->b は "value-initialized" → "zero-initialized" で 0 になるだろ。
template <class T> class C { public: T& getT() { return t_; } private: T t_; }; template <class T> class D : public C<T> { public: T& f() { return getT(); } }; 上記のコードをg++3.2でコンパイルしたところ、 Main.cpp: In member function `T& D<T>::f()': Main.cpp:11: error: there are no arguments to `getT' that depend on a template parameter, so a declaration of `getT' must be available Main.cpp:11: error: (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated) とエラーが出ました。私にはどこも間違っているように見えないのですが、これはg++のバグでしょうか? それとも上記のコードにどこか間違いがあるのでしょうか?
警告だけで、エラーではないのでコンパイルは出来ます。 ですが実行時になってからエラーが表示されます それの内容は This Application has requested the Runtime to terminate it in an unsual way. Please contact the application's support tema for more information.