编写c ++指针的不同方法有哪些?

时间:2016-03-18 04:40:36

标签: c++

就像上面的标题一样,我想我知道我的指示。然而,我对他们没有信心。我知道有四种方法可以指出正确的指针。他们是:

*foo
&foo
->foo
.foo

第四个我不知道它是否正确。

3 个答案:

答案 0 :(得分:4)

这些都不是“编写指针的方法”或不同的指针类型。

  • *foo是指针取消引用运算符
  • &foo地址 - 运算符
  • ->取消引用和访问成员运算符(((*foo).)的简写 - 作为奖励此运算符可能会超载)
  • .成员访问运算符

C ++中的不同指针(严格地说,C ++标准库)是:

  • Foo* ptr - 普通指针
  • std::auto_ptr - 已弃用。当指针离开作用域时调用析构函数。
  • std::shared_ptr - 引用计数指针。
  • std::weak_ptr - 允许在不增加引用次数的情况下使用shared_ptr的目标
  • std::unique_ptr - 与auto_ptr类似,但支持std::move,是auto_ptr的继承者。

Boost库还定义了:

  • scoped_ptr - 类似于unique_ptr但不可复制且不可移动。
  • intrusive_ptr - 与shared_ptr类似,但它允许用户使用自己的引用计数逻辑。

答案 1 :(得分:0)

指针可以指向变量(& foo)的地址,对象或地址的值(* foo)以及我正在学习的内容 - >是指结构的一个元素。

例如,让我们说变量a位于内存位置1000并包含数字7。

& a将返回1000。 *(& a)将返回7。 这里的结构和使用的代码 - >操作

package net.simplifiedcoding.myfeed;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Belal on 11/9/2015.
 */
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
    //Imageloader to load image
    private ImageLoader imageLoader;
    private Context context;

    //List to store all superheroes
    List<SuperHero> superHeroes;

    //Constructor of this class
    public CardAdapter(List<SuperHero> superHeroes, Context context) {
        super();
        //Getting all superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.superheroes_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder( final ViewHolder holder,    final int position) {
        //Getting the particular item from the list
         final SuperHero superHero = superHeroes.get(position);

        //Loading image from url
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));

        //Showing data on the views
        holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
        holder.textViewName.setText(superHero.getName());
        holder.textViewPublisher.setText(superHero.getPublisher());
        holder.textViewLikes.setText(superHero.getLikes());



        holder.custom_button.setOnClickListener(new View.OnClickListener() {
            int count;


            @Override
            public void onClick(View v) {

                count = 0;
                superHeroes.get(position).setCount(superHeroes.get(position).getCount()+1);
                holder.txtCount.setText(superHeroes.get(position).getCount());



            }

        });
        holder.txtCount.setText(superHeroes.get(position).getCount());


    }
    @Override
    public int getItemCount() {
        return superHeroes.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        //Views
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewPublisher;
        public TextView textViewLikes;
        public TextView txtCount;
        public ImageButton custom_button;

        //Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
            textViewLikes = (TextView) itemView.findViewById(R.id.textViewlikes);
            txtCount = (TextView) itemView.findViewById(R.id.txtCount);
            custom_button = (ImageButton) itemView.findViewById(R.id.custom_button);
        }
    }
}





public class SuperHero {

    //Data Variables
    private String imageUrl;
    private String name;
    private String publisher;
    private String likes;
    private int count;


    //Getters and Setters

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getLikes() {
        return likes;
    }

    public void setLikes(String likes) {
        this.likes = likes;
    }

    public int getCount() {
        return count ;
    }

    public void setCount(int count) {
        this.count = count;
    }




}




03-18 05:34:00.791 2442-2442/net.simplifiedcoding.myfeed I/art: Not late-enabling -Xcheck:jni (already on)
03-18 05:34:00.947 2442-2442/net.simplifiedcoding.myfeed W/System: ClassLoader referenced unknown path: /data/app/net.simplifiedcoding.myfeed-2/lib/x86
03-18 05:34:03.952 2442-2502/net.simplifiedcoding.myfeed D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-18 05:34:04.130 2442-2502/net.simplifiedcoding.myfeed I/OpenGLRenderer: Initialized EGL, version 1.4
03-18 05:34:04.276 2442-2502/net.simplifiedcoding.myfeed W/EGL_emulation: eglSurfaceAttrib not implemented
03-18 05:34:04.276 2442-2502/net.simplifiedcoding.myfeed W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabefa7e0, error=EGL_SUCCESS
03-18 05:34:04.312 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 10.026ms
03-18 05:34:04.763 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 6.155ms
03-18 05:34:05.751 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 124.986ms
03-18 05:34:05.947 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 47.815ms
03-18 05:34:07.241 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 127.839ms
03-18 05:34:07.795 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 176.530ms
03-18 05:34:08.173 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 9.735ms
03-18 05:34:12.374 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 937.541ms
03-18 05:34:12.376 2442-2442/net.simplifiedcoding.myfeed I/Choreographer: Skipped 56 frames!  The application may be doing too much work on its main thread.
03-18 05:34:12.403 2442-2493/net.simplifiedcoding.myfeed D/Volley: [143] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://10.0.2.2/feed.php?page=1 0x1e7db229 NORMAL 1> [lifetime=4273], [size=270], [rc=200], [retryCount=0]
03-18 05:34:12.976 2442-2445/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 288.797ms
03-18 05:34:14.183 2442-2442/net.simplifiedcoding.myfeed W/System.err: org.json.JSONException: No value for likes
03-18 05:34:14.183 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-18 05:34:14.183 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-18 05:34:14.183 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.parseData(MainActivity.java:136)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.access$000(MainActivity.java:29)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:94)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:90)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-18 05:34:14.184 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Looper.loop(Looper.java:148)
03-18 05:34:14.191 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-18 05:34:14.191 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-18 05:34:14.192 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-18 05:34:14.192 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-18 05:34:14.192 2442-2442/net.simplifiedcoding.myfeed W/System.err: org.json.JSONException: No value for likes
03-18 05:34:14.193 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-18 05:34:14.193 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-18 05:34:14.198 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.parseData(MainActivity.java:136)
03-18 05:34:14.198 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.access$000(MainActivity.java:29)
03-18 05:34:14.199 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:94)
03-18 05:34:14.199 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:90)
03-18 05:34:14.199 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
03-18 05:34:14.200 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Looper.loop(Looper.java:148)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-18 05:34:14.201 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err: org.json.JSONException: No value for likes
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-18 05:34:14.202 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-18 05:34:14.203 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.parseData(MainActivity.java:136)
03-18 05:34:14.203 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity.access$000(MainActivity.java:29)
03-18 05:34:14.203 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:94)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at net.simplifiedcoding.myfeed.MainActivity$1.onResponse(MainActivity.java:90)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-18 05:34:14.204 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-18 05:34:14.208 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.os.Looper.loop(Looper.java:148)
03-18 05:34:14.208 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-18 05:34:14.209 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-18 05:34:14.209 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-18 05:34:14.209 2442-2442/net.simplifiedcoding.myfeed W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-18 05:34:14.211 2442-2442/net.simplifiedcoding.myfeed D/Volley: [1] Request.finish: 10444 ms: [ ] http://10.0.2.2/feed.php?page=1 0x1e7db229 NORMAL 1
03-18 05:34:14.398 2442-2442/net.simplifiedcoding.myfeed W/ResourceType: No package identifier when getting value for resource number 0x00000000
03-18 05:34:14.398 2442-2442/net.simplifiedcoding.myfeed D/AndroidRuntime: Shutting down VM
03-18 05:34:14.404 2442-2442/net.simplifiedcoding.myfeed E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: net.simplifiedcoding.myfeed, PID: 2442
                                                                           android.content.res.Resources$NotFoundException: String resource ID #0x0
                                                                               at android.content.res.Resources.getText(Resources.java:312)
                                                                               at android.widget.TextView.setText(TextView.java:4417)
                                                                               at net.simplifiedcoding.myfeed.CardAdapter.onBindViewHolder(CardAdapter.java:78)
                                                                               at net.simplifiedcoding.myfeed.CardAdapter.onBindViewHolder(CardAdapter.java:21)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
                                                                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                                               at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
                                                                               at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                                               at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                                               at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                                               at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                               at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                               at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                               at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                               at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                               at android.view.View.layout(View.java:16630)
                                                                               at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                               at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-18 05:34:14.518 2442-2452/net.simplifiedcoding.myfeed I/art: Background partial concurrent mark sweep GC freed 814(168KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 13MB/17MB, paused 4.712ms total 124.488ms
03-18 05:34:14.526 2442-2452/net.simplifiedcoding.myfeed W/art: Suspending all threads took: 6.015ms
03-18 05:34:20.542 2442-2442/net.simplifiedcoding.myfeed I/Process: Sending signal. PID: 2442 SIG: 9

以及 - &gt;的使用方法操作员参考上述结构:

struct listNode {  /* self-referential structure */
char data;
struct listNode *nextPtr;
};

答案 2 :(得分:0)

*foo

不是指针。 取消引用指针的行为。

&foo

不是指针。地址。

->foo

不是指针。取消引用指向包含struct成员的foo的指针的行为。

.foo

不是任何形状或形式的指针。通过成员名struct限定foo - 值变量的行为。