Topic: none


Author: "Yuvraj Sujan" <sujan@bigfoot.com>
Date: 1999/09/07
Raw View

Consider the following case in which a virtual function having default =
arguments is overidden in the derived class and the default value
of the argument is changed. The problem is that when the derived
class overridden function is called using a base class pointer, the=20
derived class function is called BUT the default value passed is
of the base class. WHY????

( Behavior obsreved on  : VC++ 6.0, Codewarrior 3.2 )

class B
{
  public : virtual void print(int val=3D10);
     virtual void print()
     {
      cout << "In B::print. val is " << val <<  endl;
     };
    =20
};

class D : public B
{=20
 public : void print(int val=3D20) ;
     void print()
     {
      cout << "In D::print. val is " << val <<  endl;
     };
};

void main()
{
 B* pB, *pD;
 pB =3D new B;
 pD =3D new D;
 pB->print();
 pD->print();'
          =20
}

OUTPUT
In B::print. Val is 10.
In D::print. Val is 10 ; file://????????@%@$%$%#$%#@$

EXPECTED OUTPUT
In B::print. Val is 10.
In D::print. Val is 20 ;=20


Yuvraj Sujan

[ moderator's note: html attachment duplicating the contents deleted.
  Please do not submit html and do not submit attachments. Many
  newsreaders cannot handle them. -sdc ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1999/09/07
Raw View
Yuvraj Sujan wrote:
>
> Consider the following case in which a virtual function having default arguments is overidden in the derived class and the default value
> of the argument is changed. The problem is that when the derived
> class overridden function is called using a base class pointer, the
> derived class function is called BUT the default value passed is
> of the base class. WHY????

The defaulted arguments are subsituted at the call time.  It always uses
the static type of the call.  Your compiler is behaving correctly.  In
the grand scheme of things, the replacement of the overriding virtual
function is about the last thing that happens:
   - Choosing the function scope
   - determining the defaulted arguments
   - selecting which of the overloaded functions
   - checking the access class
all occurs based on the static type.  Essentially only the decision
of which code to actually execute is left for the runtime virtual
binding.


>
> void main()

Main must return INT!
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Michael Cook <mcook@cognex.com>
Date: 1995/11/20
Raw View
>>>>> "SC" == Steve Clamage <clamage@Eng.sun.com> writes:

 SC> Apart from all of that, it is unlikely to be a good idea to throw
 SC> an exception from a destructor.

Unfortunately, it's not easy in general to determine that some function being
called from a destructor would not throw.  And even if you could easily
determine that no throwing could happen, could you be sure that that would
always be true--regardless of what bit rot the program accumulates over the
years?

Ideally, the C++ language would be strong enough so that such caveats would
not be necessary.

Michael.
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]