0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

listをポインタで弄る方法

Last updated at Posted at 2020-06-18

privateなlistをポインタからアクセスする。

ポインタからのアクセスなので要素へは->を使用する。

main.cpp
# include <list>

class Data//listで使うデータをまとめたクラス
{
    public:
        float a;
        int b;
};

class MainList//listを管理するクラス
{
    private:
        std::list<Data> data;//Data型のlistを作る。
   
    public:
        std::list<Data>* GetPointer();//Data型listのポインタを返す関数。
};

std::list<Data>* MainList::GetPointer()//MainListクラスにあるGetPointer()関数を定義。
{
    return& data;//アドレスを返す。
}

void main()//C++のmain()関数。
{
    Data data;//データを作る。
    MainList main_list;//MainListを作る。
    
    main_list.push_back(data);//dataをlistにプッシュ。

    main_list.GetPointer()->begin()->a;//GetPointer()関数を通してDataクラスのa要素を見る。
    main_list.GetPointer()->begin()->b;//GetPointer()関数を通してDataクラスのb要素を見る。

    return 0;
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?