Help me out solve a problem with C++ code

Status
Not open for further replies.

booklog

Junior Member level 3
Joined
Jun 22, 2006
Messages
30
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,286
Activity points
1,441
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.
 

Re: C++ help needed

hi,

why are you assign an A class to a B class, they're totally different... unless you derive B from A...

derive it....
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…