When To Use Reference Over Pointer and Vice Versa In C++

Поділитися
Вставка
  • Опубліковано 15 гру 2024

КОМЕНТАРІ • 31

  • @JayAnAm
    @JayAnAm 5 років тому +18

    Nice, but a bit incomplete. Pointers are needed for:
    * Accessing heap memory
    * Datastructures that make use of null-pointers like linked lists or trees
    * Functions or datastructures that come from libraries that demand the use of pointers

    • @rahul-patil
      @rahul-patil 3 роки тому +1

      We can also access heap memory with reference.
      Base& baseRef_B = *(new Base());
      baseRef_B.print();
      Base& baseRef_D = *(new Derived());
      baseRef_D.print();
      Please let me know if there are any drawbacks.
      Thanks.

    • @amiladrck
      @amiladrck 3 роки тому +1

      He is covering your second point though. The third one is kind of obvious don't you think? :D

  • @Ady2xp
    @Ady2xp 7 років тому +4

    Good video as always! Thank you and keep the good job!

    • @CppNuts
      @CppNuts  7 років тому

      +Ady2xp thanks dude.

  • @anuragdogra6739
    @anuragdogra6739 6 років тому +3

    Great video with infos. Keep it up.

    • @CppNuts
      @CppNuts  6 років тому

      Thanks for appropriation!

  • @iraqiaseed
    @iraqiaseed 4 роки тому

    what is the main difference between the upcasting with reference to avoid slicing and run time polymorphism?

  • @rahul-patil
    @rahul-patil 3 роки тому

    4:16. I tried achieving it in the *main* function creating an object on *heap* and it works.
    *Any drawbacks or risks in this*?
    Base& baseRef_B = *(new Base());
    baseRef_B.print();
    Base& baseRef_D = *(new Derived());
    baseRef_D.print();
    Thanks.

  • @swatigupta6881
    @swatigupta6881 2 роки тому

    Just want to ask we can store reference of derived class to base class and type caste it later when needed. What is better? Storing it as a reference and typecasting it. or Storing it as a pointer and typecasting it. or its the same and we can choose anyone?

  • @Elliskid1995
    @Elliskid1995 3 роки тому

    Thankyou… this was my last question about pointers finally answered

    • @CppNuts
      @CppNuts  3 роки тому

      Glad i could help

  • @nithyanarayan8080
    @nithyanarayan8080 6 років тому +3

    Why its an error to use pointer in copy constructor?

    • @CppNuts
      @CppNuts  6 років тому

      show me your code.

    • @nithyanarayan8080
      @nithyanarayan8080 6 років тому +2

      #include
      #include
      using namespace std;
      class base{
      int x;
      public:
      base(int x1):x(x1){
      ;
      }
      base(const base &o){
      x=o.x;
      }
      void print(){
      cout

    • @CppNuts
      @CppNuts  6 років тому

      I don't seeany problem in this code can you tell me where the actual problem is?

    • @nithyanarayan8080
      @nithyanarayan8080 6 років тому +1

      My question is why we don't use pointer in copy constructor instead of reference?

    • @sajit1988
      @sajit1988 6 років тому +3

      @@nithyanarayan8080 I am also searching answer for the same question for looooong,
      what I see is, reference is safe..it ensure the data is valid..pointer doesnt ensure this..it can be NULL or invalid..

  • @himanshu2487
    @himanshu2487 5 років тому

    very knowledge rich videos
    form where did you study all this ?
    i want to learn more

  • @Gurl975-b9o
    @Gurl975-b9o Рік тому +1

    Can anyone please explain me copy constructor point

  • @swatigupta6881
    @swatigupta6881 2 роки тому

    can we type caste base class reference to derived class??

    • @CppNuts
      @CppNuts  2 роки тому +1

      It is safe to go up from derived class 2 base class but if you are coming from base class to derived class then you should know that base was once a derived then only you can convert.

    • @swatigupta6881
      @swatigupta6881 2 роки тому

      @@CppNuts Thank you so much.

    • @swatigupta6881
      @swatigupta6881 2 роки тому

      Just want to ask we can store reference of derived class to base class and type caste it later when needed. What is better? Storing it as a reference and typecasting it. or Storing it as a pointer and typecasting it.

    • @CppNuts
      @CppNuts  2 роки тому +1

      If you ask industries are heavily using pointer.
      Pointers give more functionality than Ref, i mean ref have some restriction which pointer don't.
      So that's y pointer is heavily used.
      There can be other reason too, but i don't remember right now.

  • @bangaruteja9020
    @bangaruteja9020 4 роки тому

    Good Video

  • @pramodkumarkar4500
    @pramodkumarkar4500 6 років тому +2

    Sooper !!!!

    • @CppNuts
      @CppNuts  6 років тому

      Thanks man!!