Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me out solve a problem with C++ code

Status
Not open for further replies.

booklog

Junior Member level 3
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():p(0){}
B(A* a):p(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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top