slicing

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
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.
Edited.
This code still doesn't compile.
 
A a=b; // error: conversion from ‘B’ to non-scalar type ‘A’ requested 
Last edited on
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.
Topic archived. No new replies allowed.