[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#171123: g++-3.2: incorrect use of default assignment operator?



Package: g++-3.2
Version: 1:3.2.1-1
Severity: normal

Hi.  Both g++-3.2 and g++-2.95 seem to have problems with the following
code.

  #include <iostream>

  struct Foo {
      int val;

      Foo(int newVal) {
          val = newVal;
      }

      Foo& operator = (const Foo& other) {
          val = 2;
      }
  };

  int main() {
      Foo foo(5);
      Foo bar = foo;

      std::cout << bar.val << std::endl;
      return 0;
  }

Under both compilers, the program compiles fine and then runs,
outputting the integer 5.

I would have expected a compile error, since the declaration
"Foo bar = foo;" tries to use the non-existant default constructor for
class Foo.

But even if it did build, I would have expected the program to output
the integer 2, since the = operator has been explicitly overridden.

What it seems to be doing is making its own default constructor and then
calling its own default assignment operator instead of the custom one
that has been defined.

Perhaps I'm just misunderstanding the finer details of the C++ specs?

Thanks - Ben.

-- System Information:
Debian Release: testing/unstable
Architecture: i386
Kernel: Linux phoenix 2.4.19-686 #1 Sun Oct 6 18:37:38 EST 2002 i686
Locale: LANG=C, LC_CTYPE=C

Versions of packages g++-3.2 depends on:
ii  gcc-3.2                       1:3.2.1-1  The GNU C compiler.
ii  gcc-3.2-base                  1:3.2.1-1  The GNU Compiler Collection (base 
ii  libc6                         2.3.1-5    GNU C Library: Shared libraries an
ii  libstdc++5-dev                1:3.2.1-1  The GNU stdc++ library version 3 (

-- no debconf information




Reply to: