私はこの質問が既に答えられていることを知っています。しかし、私が理解していることを確認したいだけです。一時的な[受動態]のアドレスを取る
ここに私のスニペットコードがあります。それはthisから来ます。私がコンパイルした場合
#include <iostream>
using namespace std;
class Base
{
void a() { cout << "a "; }
void c() { cout << "c "; }
void e() { cout << "e "; }
// 2. Steps requiring peculiar implementations are "placeholders" in base class
virtual void ph1() = 0;
virtual void ph2() = 0;
public:
// 1. Standardize the skeleton of an algorithm in a base class "template method"
virtual ~Base() = default;
void execute()
{
a();
ph1();
c();
ph2();
e();
}
};
class One: public Base
{
// 3. Derived classes implement placeholder methods
/*virtual*/ void ph1() { cout << "b "; }
/*virtual*/ void ph2() { cout << "d "; }
};
class Two: public Base
{
/*virtual*/ void ph1() { cout << "2 "; }
/*virtual*/ void ph2() { cout << "4 "; }
};
int main()
{
Base *array[] =
{
&One(), &Two()
};
for (int i = 0; i < 2; i++)
{
array[i]->execute();
cout << '\n';
}
}
、それがタイトルとしてエラーを与える:だから
error: taking address of temporary [-fpermissive]
&One(), &Two()
error: taking address of temporary [-fpermissive]
&One(), &Two()
、私はインターネットで検索してみてください。そして、彼らが言ったように:
& A()私は
error line
&One(), &Two()
を変更した場合...
を自動的に完全な表現の終了時に破壊されるテンポラリーオブジェクトを作成しています〜
new One(), new Two()
次に、動作します。
しかし、著者が書いたように私は原点のコードをどのように作っていますか?私はあなたがstd::vector<std::unique_ptr<Base>>
で、このような多型の配列を扱うことができます(上記の11と)
delete array[i];
ここで質問したい単一の質問を見つけなければなりません。あなたは現在、2つの無関係なものを求めています。 – juanchopanza