Inheritance downcast query

Discussion in 'C++' started by jayesah@gmail.com, Nov 29, 2006.

  1. Guest

    Hi All,

    I have following code. Anyone can please tell me how
    object of type class A is converted to Class B ?

    #include <iostream.h>
    class A
    {
    public:
    void operator=(int i){a=i;}

    A operator+(A& obj)
    {
    A nn;
    nn.a = a + obj.a;
    cout<<"A: operator + is called\n";
    return nn;

    }

    int a;
    };

    class B : public A
    {
    using A::eek:perator=;

    public:
    B()
    { cout<<"b's simple called\n"; };
    int b;
    };

    int main(void)
    {
    B bb1,bb2;
    bb1=3;
    bb2=4;

    B bb3;
    bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */

    cout << bb3.a;

    return 0;
    }
     
    , Nov 29, 2006
    #1
    1. Advertising

  2. * :
    >
    > I have following code. Anyone can please tell me how
    > object of type class A is converted to Class B ?


    Yes, but first, about the code.


    > #include <iostream.h>


    This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
    have this header. Use standard <iostream> plus <ostream> (and perhaps
    <istream> if input is required).


    > class A
    > {
    > public:
    > void operator=(int i){a=i;}
    >
    > A operator+(A& obj)


    That argument should be "A const& obj" unless you're intent on changing
    the actual argument object.

    As it is you can't call "+" with a constant or temporary right hand side.


    > {
    > A nn;
    > nn.a = a + obj.a;
    > cout<<"A: operator + is called\n";
    > return nn;
    >
    > }
    >
    > int a;
    > };
    >
    > class B : public A
    > {
    > using A::eek:perator=;


    Are you sure you want this assignment operator private?


    > public:
    > B()
    > { cout<<"b's simple called\n"; };


    You mean, "default constructor".


    > int b;
    > };
    >
    > int main(void)
    > {
    > B bb1,bb2;
    > bb1=3;
    > bb2=4;
    >
    > B bb3;
    > bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */


    First, there's no conversion from B to A, and this should not compile
    (have you actually tried the code?).

    Second, if you make the carte blanche "using" statement public it should
    compile, because the auto-generated A::eek:perator=( A const& ) is then
    made available in B and invoked. To avoid that you just need to be a
    bit more specific.

    E.g., in class B, "void operator=( int i ){ A::eek:perator=( i ); }"; or
    better, use a conventional member function instead of an operator.


    > cout << bb3.a;
    >
    > return 0;
    > }
    >



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 29, 2006
    #2
    1. Advertising

  3. Naresh Guest

    Re: Inheritance downcast query

    Alf P. Steinbach wrote:
    > * :
    > >
    > > I have following code. Anyone can please tell me how
    > > object of type class A is converted to Class B ?

    >
    > Yes, but first, about the code.
    >
    >
    > > #include <iostream.h>

    >
    > This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
    > have this header. Use standard <iostream> plus <ostream> (and perhaps
    > <istream> if input is required).
    >
    >
    > > class A
    > > {
    > > public:
    > > void operator=(int i){a=i;}
    > >
    > > A operator+(A& obj)

    >
    > That argument should be "A const& obj" unless you're intent on changing
    > the actual argument object.
    >
    > As it is you can't call "+" with a constant or temporary right hand side.
    >
    >
    > > {
    > > A nn;
    > > nn.a = a + obj.a;
    > > cout<<"A: operator + is called\n";
    > > return nn;
    > >
    > > }
    > >
    > > int a;
    > > };
    > >
    > > class B : public A
    > > {
    > > using A::eek:perator=;

    >
    > Are you sure you want this assignment operator private?
    >
    >
    > > public:
    > > B()
    > > { cout<<"b's simple called\n"; };

    >
    > You mean, "default constructor".
    >
    >
    > > int b;
    > > };
    > >
    > > int main(void)
    > > {
    > > B bb1,bb2;
    > > bb1=3;
    > > bb2=4;
    > >
    > > B bb3;
    > > bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */

    >
    > First, there's no conversion from B to A, and this should not compile
    > (have you actually tried the code?).
    >
    > Second, if you make the carte blanche "using" statement public it should
    > compile, because the auto-generated A::eek:perator=( A const& ) is then
    > made available in B and invoked. To avoid that you just need to be a
    > bit more specific.
    >
    > E.g., in class B, "void operator=( int i ){ A::eek:perator=( i ); }"; or
    > better, use a conventional member function instead of an operator.
    >
    >
    > > cout << bb3.a;
    > >
    > > return 0;
    > > }
    > >

    >
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?



    its a well compiled code with gcc 3.3.3 on linux.
    It also gives expected output i..e 7
     
    Naresh, Nov 29, 2006
    #3
  4. Naresh Guest

    Re: Inheritance downcast query

    Alf P. Steinbach wrote:
    > * :
    > >
    > > I have following code. Anyone can please tell me how
    > > object of type class A is converted to Class B ?

    >
    > Yes, but first, about the code.
    >
    >
    > > #include <iostream.h>

    >
    > This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
    > have this header. Use standard <iostream> plus <ostream> (and perhaps
    > <istream> if input is required).
    >
    >
    > > class A
    > > {
    > > public:
    > > void operator=(int i){a=i;}
    > >
    > > A operator+(A& obj)

    >
    > That argument should be "A const& obj" unless you're intent on changing
    > the actual argument object.
    >
    > As it is you can't call "+" with a constant or temporary right hand side.
    >
    >
    > > {
    > > A nn;
    > > nn.a = a + obj.a;
    > > cout<<"A: operator + is called\n";
    > > return nn;
    > >
    > > }
    > >
    > > int a;
    > > };
    > >
    > > class B : public A
    > > {
    > > using A::eek:perator=;

    >
    > Are you sure you want this assignment operator private?
    >
    >
    > > public:
    > > B()
    > > { cout<<"b's simple called\n"; };

    >
    > You mean, "default constructor".
    >
    >
    > > int b;
    > > };
    > >
    > > int main(void)
    > > {
    > > B bb1,bb2;
    > > bb1=3;
    > > bb2=4;
    > >
    > > B bb3;
    > > bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */

    >
    > First, there's no conversion from B to A, and this should not compile
    > (have you actually tried the code?).
    >
    > Second, if you make the carte blanche "using" statement public it should
    > compile, because the auto-generated A::eek:perator=( A const& ) is then
    > made available in B and invoked. To avoid that you just need to be a
    > bit more specific.
    >
    > E.g., in class B, "void operator=( int i ){ A::eek:perator=( i ); }"; or
    > better, use a conventional member function instead of an operator.
    >
    >
    > > cout << bb3.a;
    > >
    > > return 0;
    > > }
    > >

    >
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?



    its a well compiled code with gcc 3.3.3 on linux.
    It also gives expected output i..e 7
     
    Naresh, Nov 29, 2006
    #4
  5. Re: Inheritance downcast query

    * Naresh:
    >
    > its a well compiled code with gcc 3.3.3 on linux.
    > It also gives expected output i..e 7


    It shouldn't compile, so if you actually copied and pasted the code that
    compiled for you, then get a better compiler.

    Btw., please quote what you're responding to but /no more/. Like
    Einstein's "as simple as possible, but no simpler". Also, please don't
    quote signatures, and please don't post the same message two or more times.

    HTH. & TIA.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 29, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joris Bleys

    dynamic downcast

    Joris Bleys, Aug 1, 2003, in forum: Java
    Replies:
    9
    Views:
    2,287
    Joris Bleys
    Aug 1, 2003
  2. Jarmo

    Downcast

    Jarmo, Jul 21, 2004, in forum: Java
    Replies:
    5
    Views:
    4,100
    Jarmo
    Jul 22, 2004
  3. jimjim

    question concerning downcast

    jimjim, Mar 5, 2005, in forum: Java
    Replies:
    3
    Views:
    3,437
    jimjim
    Mar 6, 2005
  4. Steve Schlesinger

    Forced downcast - when will it work?

    Steve Schlesinger, Feb 14, 2004, in forum: C++
    Replies:
    2
    Views:
    355
    Jeff Schwab
    Feb 14, 2004
  5. Guest
    Replies:
    8
    Views:
    375
    Ron Natalie
    Apr 27, 2006
Loading...

Share This Page