Inheritance downcast query

J

jayesah

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;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
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
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.
 
N

Naresh

Alf said:
* (e-mail address removed):

Yes, but first, about the code.



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


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.



Are you sure you want this assignment operator private?



You mean, "default constructor".



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.




--
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
 
N

Naresh

Alf said:
* (e-mail address removed):

Yes, but first, about the code.



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


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.



Are you sure you want this assignment operator private?



You mean, "default constructor".



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.




--
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
 
A

Alf P. Steinbach

* 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top