Nov 4, 2015 at 6:52pm UTC
hi,
can you please tell me why 1 is printed and not 2 ?
b's properties was copied to a, doesnt it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class A{
public :
int n;
A(){n=1;}
int g(){return n;}
};
class B{
public :
int n;
B(){n=2;}
int g(){return n;}};
Main
B b;
A a=b;
cout<< a.g();
Last edited on Nov 4, 2015 at 7:09pm UTC
Nov 4, 2015 at 6:57pm UTC
There is something wrong with your code. The member data is named 'n', and then you are accessing the inexistent variable 'i'.
Also, please, write your structures in a more readable form.
Nov 4, 2015 at 7:17pm UTC
This code still doesn't compile.
A a=b; // error: conversion from ‘B’ to non-scalar type ‘A’ requested
Last edited on Nov 4, 2015 at 7:18pm UTC
Nov 4, 2015 at 7:20pm UTC
My compiler responded with:
line 15: error: 'initializing' : cannot convert from 'B' to 'A'. No constructor could take the source type, or constructor overload resolution was ambiguous
you can't assign 'b' to 'a'. They are of different, unconvertible types.