default initialization, value initialization and in-class-initialization of object not getting properly?

81 views Asked by At

You might feel code is long and I write lot of things but believe me it is quite simple.

I referred these two answers on stack-overflow for understanding this. post1 & post2

Code

#include <iostream>

class A
        {
        public:
            int k1=3;
            float f1=34.20;
            char ch1='a';
            std::string str1="bonaparte";

            void display()
            {
                std::cout<<k1<<" "<<f1<<" "<<ch1<<" "<<str1<<" xx\n";
            }

        };

class B
{
    public:
        int k2;
        float f2;
        char ch2;
        std::string str2;

        void display()
        {
            std::cout<<k2<<" "<<f2<<" "<<ch2<<" "<<str2<<" xx\n";
        }

};

A a1;
B b1;

A *p1=new A;
B *q1=new B;
int main()
{
    A a2, a3{};
    B b2, b3{};

    A *p2=new A;
    A *p3=new A{};
    A *p4=new A();

    B *q2=new B;
    B *q3=new B{};
    B *q4=new B();

    a1.display();
    a2.display();
    a3.display();
    p1->display();
    p2->display();
    p3->display();
    p4->display();

    std::cout<<"\n\n";

    b1.display();
    b2.display();
    b3.display();
    q1->display();
    q2->display();
    q3->display();
    q4->display();


}

Output

3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx
3 34.2 a bonaparte xx


0 0    xx
13965136 0 ╠  xx
0 0    xx
38872208 0 p  xx
38872208 0 p  xx
0 0    xx
0 0    xx

Class B

b1 is value initialized precisely zero initialized.

b2 is default initialized means no initialized.

b3 is brace initialized means value initialized which leads to zero initialized.

I am not able to create b4() like q4 (not exact like but you know what I mean) as it leads function definition rather than declaration of object( Most vexing parse)

I think q1 should value initialized but after seeing output it's not matches what I think.

q2 is default initialized means no initialized.

q3 is brace initialized means value initialized which leads to zero initialized.

q4 calls constructor but there is no user defined constructor so it should default initialized means no initialized.

I know q1, q2, q3, q4 are pointer but just to easy I am using above language for ex. q3 is brace initialized. but I think you know what I mean.

What I believe regarding b1, b2, b3 turns out to be true so I don't have any question regarding that might be it is just mere coincidence so correct me if I am wrong.

Regarding q1, q2, q3, q4

what I assume for b1 I think it should work for q1 too but it not happens so please explain that.

Regarding q2 what I think matches so no problem but still correct me if I am wrong.

Now there is confusion with q3 & q4. I think both are calling for constructors but I am not sure. I assume both calling constructor but we have not user defined constructors so it should default initialized means un-initialized isn't that right ? so how come both are value initialized precisely zero initialized.

Class A

So if we do in-class initialization no matter where we declare object it's going to initialize with what we provide, I feel it's right but just want to confirm and please add if you would like to tell me bit more about this.

0

There are 0 answers