質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1500閲覧

スマートポインタのクラスの配列の使い方が分かりません

gemfighter

総合スコア38

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2020/11/25 12:55

編集2020/11/25 14:41

C++

1#include <iostream> 2#include <fstream> 3#include <vector> 4#include <algorithm> 5#include <functional> 6#include <string> 7#include <iomanip> 8 9using namespace std; 10 11//基底クラス 12class Bottom 13{ 14public: 15 short x1; //描画領域 左上 X座標 2B 16 short y1; //描画領域 左上 Y座標 2B 17 short x2; //描画領域 右下 X座標 2B 18 short y2; //描画領域 右下 Y座標 2B 19 long foreColor; //前景色 4B 20 long backColor; //背景色 4B 21 22 int recg; //派生クラス認識 23 24 void get(char h2, char h3, char h4, char h5, char h6, char h7, char h8, char h9, char h10, char h11, char h12, char h13, char h14, char h15, char h16, char h17); 25 26 //値取得 27 virtual void get2(); 28 29 void get3(int i); 30 31 //表示 32 virtual void LK(); 33 34 //コンストラクタ 35 Bottom(); 36 37 //デストラクタ 38 virtual ~Bottom(); 39}; 40 41//直線 42class StraightLine : public Bottom 43{ 44public: 45 46 char lineKind; //線種(0:実線、1:点線) 47 short startX; //描画開始 X座標 2B 48 short startY; //描画開始 Y座標 2B 49 short endX; //描画終了 X座標 2B 50 short endY; //描画終了 Y座標 2B 51 52 //コンストラクタ 53 StraightLine(); 54 55 //デストラクタ 56 ~StraightLine(); 57 58 //値取得 59 void get2(char h18, char h20, char h21, char h22, char h23, char h24, char h25, char h26, char h27); 60 61 //表示 62 void LK(); 63}; 64 65//基底コンストラクタ 66Bottom::Bottom() 67{ 68 69} 70 71//基底デストラクタ 72Bottom::~Bottom() 73{ 74 75} 76 77//基底表示 78void Bottom::LK() { 79 cout << "ボトムやで" << endl; 80} 81 82void Bottom::get(char h2, char h3, char h4, char h5, char h6, char h7, char h8, char h9, char h10, char h11, char h12, char h13, char h14, char h15, char h16, char h17) { 83 //unsigned使ってるのは、符号なしでbitを移動させたいから 84 85 //描画領域 左上 X座標 86 x1 = (unsigned char)h3; 87 x1 <<= 8;//左に8ビット移動 88 x1 = (x1 | (unsigned char)(h2 & 0xff));//データを連結 89 90 //描画領域 左上 Y座標 91 y1 = (unsigned char)h5; 92 y1 <<= 8;//左に8ビット移動 93 y1 = (y1 | (unsigned char)(h4 & 0xff));//データを連結 94 95 //描画領域 右下 X座標 96 x2 = (unsigned char)h7; 97 x2 <<= 8;//左に8ビット移動 98 x2 = (x2 | (unsigned char)(h6 & 0xff));//データを連結 99 100 //描画領域 右下 Y座標 101 y2 = (unsigned char)h9; 102 y2 <<= 8;//左に8ビット移動 103 y2 = (y2 | (unsigned char)(h8 & 0xff));//データを連結 104 105 //前景色 106 foreColor = (unsigned char)h13; 107 foreColor <<= 8;//左に8ビット移動 108 foreColor = (foreColor | (unsigned char)(h12 & 0xff));//データを連結 109 foreColor <<= 8;//左に8ビット移動 110 foreColor = (foreColor | (unsigned char)(h11 & 0xff));//データを連結 111 foreColor <<= 8;//左に8ビット移動 112 foreColor = (foreColor | (unsigned char)(h10 & 0xff));//データを連結 113 114 //背景色 115 backColor = (unsigned char)h17; 116 backColor <<= 8;//左に8ビット移動 117 backColor = (backColor | (unsigned char)(h16 & 0xff));//データを連結 118 backColor <<= 8;//左に8ビット移動 119 backColor = (backColor | (unsigned char)(h15 & 0xff));//データを連結 120 backColor <<= 8;//左に8ビット移動 121 backColor = (backColor | (unsigned char)(h14 & 0xff));//データを連結 122} 123 124//基底値取得 125void Bottom::get2() { 126 127} 128 129void Bottom::get3(int i) { 130 recg = i; 131} 132 133//直線コンストラクタ 134StraightLine::StraightLine() 135{ 136 x1 = 0; 137 y1 = 0; 138 x2 = 0; 139 y2 = 0; 140 foreColor = 0; 141 backColor = 0; 142 recg = 0; 143 lineKind = 0; 144 startX = 0; 145 startY = 0; 146 endX = 0; 147 endY = 0; 148} 149 150//直線デストラクタ 151StraightLine::~StraightLine() 152{ 153 154} 155 156 157 158//直線値取得 159void StraightLine::get2(char h18, char h20, char h21, char h22, char h23, char h24, char h25, char h26, char h27) 160{ 161 lineKind = h18;//線種 162 163 //描画開始 X座標 164 startX = (unsigned char)h21; 165 startX <<= 8;//左に8ビット移動 166 startX = (startX | (unsigned char)(h20 & 0xff));//データを連結 167 168 //描画開始 Y座標 169 startY = (unsigned char)h23; 170 startY <<= 8;//左に8ビット移動 171 startY = (startY | (unsigned char)(h22 & 0xff));//データを連結 172 173 //描画終了 X座標 174 endX = (unsigned char)h25; 175 endX <<= 8;//左に8ビット移動 176 endX = (endX | (unsigned char)(h24 & 0xff));//データを連結 177 178 //描画終了 Y座標 179 endY = (unsigned char)h27; 180 endY <<= 8;//左に8ビット移動 181 endY = (endY | (unsigned char)(h26 & 0xff));//データを連結 182} 183 184//直線表示 185void StraightLine::LK() { 186 cout << "描画領域(X1,Y1,X2,Y2) 前景色,背景色 線種   描画開始,終了" << endl; 187 cout << "\"直線 [" << setfill(' ') << right << setw(4) << dec << x1 << "," << setfill(' ') << right << setw(4) << dec << y1 << "]" 188 << " - [" << setfill(' ') << right << setw(4) << dec << x2 << ", " << setfill(' ') << right << setw(4) << dec << y2 << "]" 189 << " [0x" << setfill('0') << setw(6) << hex << foreColor << "] [0x" << setfill('0') << setw(6) << hex << backColor << "]"; 190 191 192 if (lineKind == 0) { 193 cout << " 実線 "; 194 } 195 else { 196 cout << " 点線 "; 197 } 198 cout << "[" << setfill(' ') << dec << setw(4) << startX << ", " << setfill(' ') << dec << setw(4) << startY << "]"; 199 cout << "[" << setfill(' ') << dec << setw(4) << endX << ", " << setfill(' ') << dec << setw(4) << endY << "]"; 200 201 202 //printf("[%4d,%4d] - [%4d,%4d]\"\n",startX, startY, endX, endY); 203 cout << endl; 204 cout << endl; 205} 206 207int main(){ 208 //直線用インスタンス(この配列に要素入れる) 209 shared_ptr<StraightLine> straightine(new StraightLine)[2]; 210 211 //直線出力用インスタンス 212 std::vector<std::shared_ptr<Bottom>> bases;//全部入れるテーブル 213 shared_ptr<StraightLine> straightine2(new StraightLine); 214 215  //ここの処理で入れる 216 for(int i = 0; i < 2; i++){ 217 //インスタンスに値を格納 218 straightine->get(27, 1, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0); 219 straightine->get2(0, 0, 0, 0,0, 0, 0, 0, 0); 220 straightine->get3(1); 221 //ベクターに値をプッシュ 222 bases.push_back(straightine); 223 224 //もともとはここでポインタのアドレスをインクリメントしていた。 225 226 227 } 228 229 230 231 232 233 234 for (auto it = std::begin(bases); it != std::end(bases); ++it) 235 { 236 //直線出力 237 if (dynamic_cast<StraightLine>(*it) != 0) 238 { 239 straightine2 = dynamic_cast<StraightLine*>(*it); 240 straightine2->LK(); 241 } 242 } 243 244 return 0; 245}

インスタンスをshared_ptrの配列ににしましたが、それぞれの配列の中に要素を入れ、その後、ベクターのテーブルに入れた後、イテレータで回してそれぞれ出力したいのですが、方法が分かりません。
どなたか教えてくださると幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yumetodo

2020/11/25 14:34

そもそもstraightineが定義されていない問題が一つと、shard_ptrつかってるんだからdeleteは勝手にしてくれるやろ問題となんでshared_ptrにポインタ入れてとるんや問題が
yumetodo

2020/11/25 15:22

頑張って読もうと思ったけどなんでgetって名前でメンバー書き換えているのか皆目理解できないしget, get2, get3って名前の時点で頭に入ってこないし、straightineとbasesは何がちゃうねんと思うし、そのdynamic castの使い方未定義動作だとおもうのでstd::dynamic_pointer_cast使えって思うし`dynamic_cast<StraightLine>(*it) != 0`とは???という思いで溢れていて読む気が0になってしまったのでさじを投げます。
guest

回答2

0

やりたいのはこんな感じの事ですか?

C++

1 2int main() { 3 std::vector<std::shared_ptr<Bottom>> bases;//全部入れるテーブル 4 5 for (int i = 0; i < 2; i++) { 6 std::shared_ptr<StraightLine> straightine = std::make_shared<StraightLine>(); 7 straightine->get(27, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 8 straightine->get2(0, 0, 0, 0, 0, 0, 0, 0, 0); 9 straightine->get3(1); 10 bases.push_back(straightine); 11 } 12 13 // dynamic_pointer_cast確認用にStraightLine以外のものも入れておく 14 bases.push_back(std::make_shared<Bottom>()); 15 16 for (auto base : bases) 17 { 18 std::shared_ptr<StraightLine> straightine = std::dynamic_pointer_cast<StraightLine>(base); 19 if (straightine) 20 { 21 // StraightLineのみに存在するメソッドの呼び出しや追加の処理を行いたい? 22 //straightine->doSomething(); 23 24 straightine->LK(); 25 } 26 else 27 { 28 // dynamic_pointer_cast確認用 29 base->LK(); 30 } 31 } 32 33 return 0; 34}

投稿2020/11/25 15:24

SHOMI

総合スコア4079

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

どうしてもスマートポインタで配列を使いたいなら、こんな感じかな。
スマートポインタだと配列の途中の要素を指せないので、参照は通常ポインタで。

C++

1int main() { 2 //直線用インスタンス(この配列に要素入れる) 3 shared_ptr<StraightLine[]> straightine(new StraightLine[2]); 4 5 //直線出力用インスタンス 6 std::vector<Bottom*> bases;//全部入れるテーブル 7 StraightLine* straightine2 = nullptr; 8 9 //ここの処理で入れる 10 for (int i = 0; i < 2; i++) { 11 //インスタンスに値を格納 12 straightine[i].get(27, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 13 straightine[i].get2(0, 0, 0, 0, 0, 0, 0, 0, 0); 14 straightine[i].get3(1); 15 //ベクターに値をプッシュ 16 bases.push_back(&straightine[i]); 17 } 18 19 for (auto it = std::begin(bases); it != std::end(bases); ++it) 20 { 21 //直線出力 22 if (dynamic_cast<StraightLine*>(*it) != 0) 23 { 24 straightine2 = dynamic_cast<StraightLine*>(*it); 25 straightine2->LK(); 26 } 27 } 28 29 return 0; 30}

ただ、これならstraightineをvectorにしてしまったほうが良い気がする。

C++

1 vector<StraightLine> straightine(2);

投稿2020/11/25 15:42

actorbug

総合スコア2224

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yumetodo

2020/11/25 16:49

仮想メンバ関数LK(これも命名がよーわからん)を呼び出すんだったらdynamic cast、いる?という気がしてきています・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問