有关引用和指针的问题

时间:2019-01-11 14:08:51

标签: c++ pointers reference

考虑这个简单的类:

function counter() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheets()[0];         // Get the first worksheet.
  var range = sheet.getRange('A1:A1');   // Get the content of the first cell "abc; def; 4f5"
  range.activate();
  var content = range.getValue();
  content = content.replace(/;+/g, ';'); // strip double ;;
  var count = content.split(';').length;
  Logger.log("The content %s has %s words seperated by semicolons.", content, count);
} 

如果我写:

//songs activity

public class SongsActivity extends AppCompatActivity {

ListView mySongsListView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_songs);

    doStuff();

}
    public void doStuff ()
    {
        mySongsListView = (ListView) findViewById(R.id.songsListView);

        getMusic();


        mySongsListView.setAdapter(new SongsAdapter(getApplicationContext()));

        mySongsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //open music player to play desired song
            }
        });
    }


    public void getMusic ()
    {
        ContentResolver contentResolver = getContentResolver();
        Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor songCursor = contentResolver.query(songUri, null, null, null, null);

        if (songCursor != null && songCursor.moveToFirst()) {



            int songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int songAlbum = songCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);

            do {

                Song newSong =  new Song();

                newSong.songTitle = songCursor.getString(songTitle);
                newSong.artistName = songCursor.getString(songArtist);
                newSong.artistAlbum = songCursor.getString(songAlbum);

                DataStorage.songs.add(newSong);


            }
            while (songCursor.moveToNext());
        }
    }
}


// songs adapter

public class SongsAdapter extends BaseAdapter {

Context myContext;
LayoutInflater myInflater;

public SongsAdapter(Context context) {
    this.myContext = context;
    myInflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return DataStorage.songs.size();
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    if (view == null)
    {
        view = myInflater.inflate(R.layout.song_list_view_single_item, viewGroup, false);
    }

    TextView songTitle = (TextView) view.findViewById(R.id.song_title);
    TextView songArtist = (TextView) view.findViewById(R.id.song_artist);
    ImageView songArt = (ImageView) view.findViewById(R.id.song_Tmb);

    Song song = DataStorage.songs.get(position);

    songTitle.setText(song.songTitle);
    songArtist.setText(song.artistName + " - " + song.artistAlbum);

    return view;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

}


// data storage
public static ArrayList<Song> songs = new ArrayList<Song>();

是否会在堆上创建一个新的class Test { public: Test() { data = 0; }; Test(int integer); int getData() { return data; } ~Test(); private: int data; }; 对象,然后将其复制到临时Test t = *new Test; 对象Test?会发生内存泄漏吗?

为什么这无效?

Test

但这不是:

t

我知道我可以简单地Test* t = new Test(0); int i = *t.getData(); 做,但是我不明白为什么上面的例子不起作用?

2 个答案:

答案 0 :(得分:2)

  

是否会在堆上创建一个新的Test对象,然后将其复制到临时Test对象t?会发生内存泄漏吗?

是和是。


  

为什么我不能这样做:

member of object access operator.)的优先级高于*。您可以执行(*t).getData()

答案 1 :(得分:0)

  

为什么我不能这样做:

Test* t = new Test(0);
int i = *t.getData();

您不能这样做,因为.的优先级高于*。您可以使用()显式排序操作,也可以使用->(内置的简写运算符):

int i = t->getData();