booklog
Junior Member level 3
I have following code.
#include<iostream>
using namespace std;
class A{
public:
int x;
A():x(10){}
};
class B{
public:
A* p;
B()(0){}
B(A* a)(a){cout<<"Inside B(A* a)"<<endl;}
};
int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}
when i compiled this, the compiler didn't complain.
The doubt is, in main when I assign the object b to A's pointer (through new A),
the compiler should complain because i don't have any operator overloaded for "=". Instead it calls the constuctor "B(A* a)", which is wrong since i am assigning, not initializing b.
#include<iostream>
using namespace std;
class A{
public:
int x;
A():x(10){}
};
class B{
public:
A* p;
B()(0){}
B(A* a)(a){cout<<"Inside B(A* a)"<<endl;}
};
int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}
when i compiled this, the compiler didn't complain.
The doubt is, in main when I assign the object b to A's pointer (through new A),
the compiler should complain because i don't have any operator overloaded for "=". Instead it calls the constuctor "B(A* a)", which is wrong since i am assigning, not initializing b.