www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Type conversions

↑ ↓ ← 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
↑ ↓ → "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