www.digitalmars.com         C & C++   DMDScript  

c++ - Type conversions

reply Christof Meerwald <cmeerw web.de> writes:
#include <stdio.h>

struct B;

struct A
{
  virtual B &get() = 0;

  operator B&()
  {
    printf("A::operator B&\n");
    return get();
  }
};

struct B
  : public A
{
  virtual B &get()
  {
    return *this;
  }
};

struct C
  : public B
{ };


void f(B &b)
{ }


int main()
{
  B b;
  C c;

  printf("A &a = b\n");
  A &a = b;

  printf("(B &) a\n");
  f((B &) a);

  printf("a.get()\n");
  f(a.get());

  printf("b\n");
  f(b);

  printf("c\n");
  f(c);


  return 0;
}


When compiled with DM, I get the following output:

A &a = b
A::operator B&
(B &) a
A::operator B&
a.get()
b
c
A::operator B&

I have no idea why DM uses "A::operator B&" to convert b to A&. And DM also
shouldn't use the user-defined operator to convert c to B& (see 13.3.3.2
Ranking implicit conversion sequences [over.ics.rank], paragraph 2, of the
C++ Standard: "a standard conversion sequence is a better conversion
sequence than a user-defined conversion sequence or an ellipsis conversion
sequence...").

BTW, I get the expected result when using gcc 3.0:

A &a = b
(B &) a
A::operator B&
a.get()
b
c


bye, Christof

-- 
http://cmeerw.org                                 JID: cmeerw jabber.at
mailto cmeerw at web.de

...and what have you contributed to the Net?
Dec 11 2002
parent "Walter" <walter digitalmars.com> writes:
At the moment, I decided to replace the preprocessor stage. The code in
there is so old and byzantine, it no longer looked like any productive
changes could be made to it. I hope to correct the longstanding problems
with it.
Dec 11 2002