Topic: casting down from base to derive


Author: ywong@promis.com (Ying Wong)
Date: Sat, 16 Oct 1993 05:04:09 GMT
Raw View
Questions about using conversion operator to cast-down from a base instance
into a derived instance:

The casting has to be a TRUE cast inorder to be able to call the right
virtual function.

The following program is a strip down version of the original one. A
lot of details (i.e. constructors, attributes) are obmitted.

The questions are listed at the end of this short program.

// -------------------- START ---------------------------------
class B ; // forward declaration

class A {
 public:
  operator B * (); // conversion operator to a derived class.

  virtual void vclassName() {  // <== This is a Virtual function
            cout << "I am an instance of class A" <<endl;
        }
};

class B : public A {
 public:
  virtual void vclassName() {
   cout << "I am an instance of class B" <<endl;
  }
};

A::operator B*()
{
 B *bptr=new B();
 return bptr;
};

main()
{
 A a, *aPtr;
 B b, *bPtr;

 aPtr=bPtr; // inplicit conversion.
 aPtr->vclassName(); // output "I am an instance of class B"
      // which is OK.

 bPtr=(B *)aPtr; // output "I am an instance of class A"
     // but What I expected to see is "I am an instance of class B"

 bPtr=(B *)*aPtr; // output "I am an instance of class B"
      // The conversion operator is invoked in this case
      // where is OK.

}

My Question is :
 1)
 is conversion operator only get fired when the class instance is not a
 pointer ?

 If yes, is it mentioned in B. Stroustrup C++ Programming Language, or
 Stanley L. C++ Primer ? If yes, please specifies the page number.

 2)
 Is there a better way to do cast-down than the one I used ?

Thanks
ying
--

Have a great day!

-------------------------------------------------------------------------------




Author: ywong@promis.com (Ying Wong)
Date: Mon, 18 Oct 1993 03:18:54 GMT
Raw View
Questions about using conversion operator to cast-down from a base instance
into a derived instance:

The casting has to be a TRUE cast inorder to be able to call the right
virtual function.

The following program is a strip down version of the original one. A
lot of details (i.e. constructors, attributes) are obmitted.

The questions are listed at the end of this short program.

// -------------------- START ---------------------------------
class B ; // forward declaration

class A {
 public:
  operator B * (); // conversion operator to a derived class.

  virtual void vclassName() {  // <== This is a Virtual function
            cout << "I am an instance of class A" <<endl;
        }
};

class B : public A {
 public:
  virtual void vclassName() {
   cout << "I am an instance of class B" <<endl;
  }
};

A::operator B*()
{
 B *bptr=new B();
 return bptr;
};

main()
{
 A a, *aPtr;
 B b, *bPtr;

 aPtr=bPtr; // inplicit conversion.
 aPtr->vclassName(); // output "I am an instance of class B"
      // which is OK.

 bPtr=(B *)aPtr; // output "I am an instance of class A"
     // but What I expected to see is "I am an instance of class B"

 bPtr=(B *)*aPtr; // output "I am an instance of class B"
      // The conversion operator is invoked in this case
      // where is OK.

}

My Question is :
 1)
 is conversion operator only get fired when the class instance is not a
 pointer ?

 If yes, is it mentioned in B. Stroustrup C++ Programming Language, or
 Stanley L. C++ Primer ? If yes, please specifies the page number.

 2)
 Is there a better way to do cast-down than the one I used ?

Thanks
ying
--

Have a great day!

-------------------------------------------------------------------------------