What Are All Those Places Where Initializer List Is Must In C++?

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

КОМЕНТАРІ • 57

  • @sukumarrepaka9399
    @sukumarrepaka9399 7 років тому +15

    In the 5th point,
    Base(int _x)
    {
    this->_x = _x;
    }
    will also work. Just wanted to add this point.
    BTW, nice explanations.

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

      Correct!!
      Thanks for adding the point.

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

      As If I know -> this is used when refering or in case if pointer ...which case is this here ?

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

      @@behindthescene4406 we are referring to _x of this class and not referring to the local variable _x .... Please correct me , if wrong

    • @Panthera-Uncia
      @Panthera-Uncia 3 роки тому

      ​@@bhupeshpattanaik7150 "this" keyword is used when referring to the Class member in order to distinguish it from the local variable that is in the function parameter which happens to use the same variable name.
      i.g.
      Class Number {
      int x;
      int y;
      public:
      void addXandY (int x, int y)
      {
      this - > x = x; (The int x in the class is assigned to the int x in the parameter of the function addXandY)
      this - > y = y; (The int y in the class is assigned to the int y in the parameter of the function addXandY)
      cout

    • @JKA-sf7ll
      @JKA-sf7ll 2 роки тому

      Yes..

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

    11:25
    class MyClass{
    public:
    MyClass(Base b);
    }
    MyClass mc(b);
    Here, when the copy constructor is called how does object creation / (Memory allocation) takes place *without* calling *default* constructor?
    Thanks for demonstrating all the points.

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

    class Base
    {
    const int _x;
    int& _y;
    public:
    Base(int a) : _x{a}, _y{a}{ cout

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

      Thanks Nirankush.

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

    Some people are very careful to distinguish between C++ initialization and C++ assignment. Initialization happens once. Assignment might happen more than once. Of course if you choose to you can use the terms more leniently, as though not writing software. I find that confusing. I think you may want to use the words C++ initialize and C++ assign more strictly. Initialization and Assignment are not the same action. Both can modify a value. A references and const must attain initial value with initialization ... and zero assignments. Some other fine points are omitted.

  • @santoshsingh-ni5de
    @santoshsingh-ni5de 6 років тому +4

    For reference data member, constructor in second point should be : Base(int &x) :_x{x} {}

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

      Plz specify video timing, while asking question about the video so that it will be easier for me to answer otherwise i have watch it again and it will be of so much time waste.

    • @ks-op8pe
      @ks-op8pe 2 роки тому +1

      Yes, you are correct! I mean it's Base(int &x) :_x{x} {}

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

    In the 4th point , we can do this also
    #include
    using namespace std;
    class base
    {
    int x;
    public:
    base (int a):x{a}{}
    };
    class child:public base
    {
    int y;
    public:
    child(int i,int j):base(j)
    {
    y=i;
    }
    };
    int main()
    {
    base b1(31);
    child c1(2,3);
    return 0;
    }

  • @janardhanreddy1180
    @janardhanreddy1180 5 років тому +3

    Last point is amazing hattsoff to your efforts

    • @CppNuts
      @CppNuts  5 років тому +1

      Thanks man..

  • @shibisaketh
    @shibisaketh 7 років тому +2

    one more optimization: call by reference and avoid 1 copy constructor call.
    MyClass(const Base& b) :_b{ b }

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

      ShibiN B Hi..
      Correct... making reference call save you from making temp obj.

    • @shibisaketh
      @shibisaketh 7 років тому +1

      This video tutorials are very useful. Good attempt.. waiting for more.Thanks :)

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

      ShibiN B Hi..
      Thanks dude, yes will keep on uploading. :)

  • @harshbhavsar8588
    @harshbhavsar8588 Місяць тому

    At 6:30, still having one confusion, if we write a = x, compiler would provide copy constructor but it throws error as default constructor not available for 'One a'. Question is, Why compiler won't provide default constructor for One?

  • @yogeshasati6210
    @yogeshasati6210 8 місяців тому

    @ccpnuts 13:41 how " base copy constructor" called 2 times ..what is mean by inplace constructor..?thanks in advance

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

    You can use C++11 in-class default member initializer as Sonu Lohani wrote.
    If you assign it to the return value of a function, you can set it from outside at run time too. :)
    class Base {
    public:
    Base() = default; // declare it as the default constructor.
    void print() { cout = 0 && number

  • @VikashKumar-uh4kx
    @VikashKumar-uh4kx 3 роки тому

    Nice explanation. 👍
    Return *this should be there in assignment operator code.

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

    Your kind of videos are good, explaining lot of things about what's happening behind the code actually.

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

      Glad you liked it !!

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

    For a guy who comes from Python, this is an entirely wholesome, intimidating and strong level of language

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

    so complicated. nicely done.

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

      Thanks man!!

  • @xinli6243
    @xinli6243 5 років тому +1

    Hi, I have a question on example 1 (3:08). After you call Base(10), variable _x is actually referring to an x which is a local variable in Base constructor. At this moment, x has been recycled since it is out of scope. How can you print _x ? I thought you might get an SegV error but actually you didn't. I got confused. Can you tell me what's going on in your code?

    • @soulimanemammar2909
      @soulimanemammar2909 5 років тому +1

      You are right ! you can't initialize a reference member with a local variable. it's an undefined behavior

    • @ks-op8pe
      @ks-op8pe 2 роки тому

      Base(int& x) :_x{x} {}

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

    Base(const Base & obj){this->x = obj._x;cout

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

    At 6:16 . Why Default Constructor is a must.
    You said to get object of class One in class Two, we need a default constructor in class One (In case we don's use Initializer list)
    But Why is it so
    Even without Default constructor we can create an object 'a' of class One inside class Two.
    Then why can't we initialise a directly with x like:
    Two(One x) : a{x} {};

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

      the thing what I understood is, we should not create another "One" object in "Two" as because we are already passing a "One" object in "main" function. So address values may be ambiguous. Hence for not making an object, we have to use object of "One" either by using just default constructor or if not default constructor (then use initializer list)...
      If I'm wrong correct me pls.👉👈

  • @sonulohani
    @sonulohani 7 років тому +1

    In c++11 we can even initialize read only memory in class definition also...
    ex: class abc { const int x=10;};

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

      Sonu Lohani Hi..
      Yes it can, I pointed how you can set from outside at run time. :)

  • @bigWordsHurt47
    @bigWordsHurt47 5 років тому +1

    hey what is the color scheme you use for your editor?

    • @CppNuts
      @CppNuts  5 років тому +2

      sublime monokai theme

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

    What is the difference between _x(param) and _x{param} in the initializer list? I was previously used to the () syntax.

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

      The author CppNuts has made a lecture explaining this concept for my request.
      ua-cam.com/video/P-WsU-hBf7c/v-deo.html

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

    As I know, you cannot use :
    class Foo {
    int x;
    public:
    Foo(int x) : x{x} // you cannot use x{x} here,
    /*instead you should use:*/
    Foo(int x) : x(x)
    {
    }
    };
    I use Netbeans and I am saying this by trying the code in Netbeans.

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

      Hi Eagle.Eye, we can actually use it. It is used for uniform initialisation.
      This is the link of online compiler where you can compile and check if it is working.
      ideone.com/jIDuSK

  • @bensalemmohamedabderrahman5844
    @bensalemmohamedabderrahman5844 4 роки тому +1

    lets say i have an array of objects named "writer" inside private of class named "book" i want to initialize this array inside the constructor of book how can i do it?

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

      Watch the video again and try to find your answer in this.

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

      @@CppNuts i don't know how to initialize when its an array it doesn't work for me

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

    In the second point I got a warning on MacBook with g++ -std=c++14 option:
    warning: binding reference member 'x_' to stack allocated parameter 'x' [-Wdangling-field]
    class Base1 {
    int &x_;
    public:
    Base1(int x): x_{x} {}
    void print() {cout

  • @anand1kum1
    @anand1kum1 5 років тому +1

    how to initialize reference data member of a class in default constructor.
    class base{
    int& _bg;
    public:
    base():_bg(0){}; //Gives error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int'|
    };

    • @CppNuts
      @CppNuts  5 років тому +1

      You can not have default constuctor if u have reference data member.
      Because you have to initialize ref data member while creating object, while default ctor doesn't take anything.

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

    Why you used variable name. _X and not X , is it any writting convention used for intialiser lists ?

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

      No, it is used to differentiate between data members and normal variables in member function.

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

      @@CppNuts ok .... thanks
      Nice channel for those who like CPP
      Best channel I found for CPP 😍👌👍

  • @黎銘-s9n
    @黎銘-s9n 4 роки тому

    Is C++ the second official language in India? If so, I'll consider moving to India.

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

    these south indian accent is so funny, stop using it. Hard to understand