[comp.lang.c++] Strange C++ compiler messages

brione@hpfcmdd.hp.com (Brion Emde) (03/26/91)

Hello fellow C++'ers,

  This is an example program that illustrates a problem I have been seeing
with passing derived class pointers both by reference and by explicit
pointer-to-pointer arguments to a function defined to take a pointer to base
class (the push calls in main).  The complilers I tried this on both complain.
I am confused by the messages from the compilers.  I thought that derived
class pointers are implicitly converted to base class pointers whenever
required.  But the ATT-based compiler complains about passing the pointer
by reference, and allocates a temporary variable for the reference, so that
my pointer is not updated.  Also the ATT compiler complains that it can't 
find the proper function to call for the explicit pointer-to-pointer version 
of the call (see below), the same complaint generated by the g++ compiler.

  Am I totally off base here, or should this example work?  I submit it
to the C++ community for the definitive answer.

Thank you in advance,

Brion Emde
Hewlett-Packard Mechanical Design Division
MS 110
3404 E. Harmony Road
Ft. Collins, CO 80525
work phone - (303) 229-4838

------------------------CUT HERE-------------------------------------------
// Stack example with two alternate next pointer passing
//
// It seems to me that the implicit conversion from derived class pointer to 
// base class pointer is not occuring correctly in the push() calls in main().
//
// Messages from HP C++ B2400 X.02.18/ATT 2.1 C++ compiler
//
// CC: "ref_ptr_prob.C", line 55: warning: temporary used to initialize 
//      reference; no changes will be propagated to actual argument (145)
// CC: "ref_ptr_prob.C", line 56: error: bad argument list for 
//      STACK_NODE::push() (no match against any  STACK_NODE::push()) (1261)
//
// Messages from GNU g++: 
//
// ref_ptr_prob.C: In function int main ():
// ref_ptr_prob.C:56: bad argument 0 for function 
// `STACK_NODE::push (class STACK_NODE **)' (type was class INT_STACK_NODE **)
//

class STACK_NODE
{
 private:
  STACK_NODE *next_ptr;
  
 public:
           STACK_NODE() : next_ptr(0) {}
  virtual ~STACK_NODE() {}

  void                     push(STACK_NODE* &); // pass pointer by reference
  void                     push(STACK_NODE* *); // pass pointer to pointer
};

void STACK_NODE::push(STACK_NODE* &stack)
{
  next_ptr = stack;
  stack = this;
}

void STACK_NODE::push(STACK_NODE* *stackp)
{
  next_ptr = *stackp;
  *stackp = this;
}

// USELESS derived class for example
class MY_STACK_NODE : public STACK_NODE {
};

main()
{
  MY_STACK_NODE *stack = 0; 
  MY_STACK_NODE *stn1 = new MY_STACK_NODE;
  MY_STACK_NODE *stn2 = new MY_STACK_NODE;

  stn1->push(stack); 
  stn2->push(&stack); 
}