[gnu.g++.bug] private const inited member bug

wasg@RICE.EDU (Eric Salituro) (09/16/89)

Here is a bug report from one of my users. Please respond directly to
him.

----- Begin Included Message -----

From dougm Fri Sep 15 13:17:51 1989
Received: by titan.rice.edu (AA07570); Fri, 15 Sep 89 13:17:49 CDT
Date: Fri, 15 Sep 89 13:17:49 CDT
From: Doug Moore <dougm>
Message-Id: <8909151817.AA07570@titan.rice.edu>
To: wasg
Subject: private const inited member bug
Status: R

Here is my bug report to bug-g++@prep.ai.mit.edu.  I would appreciate
it if you could answer the questions about the g++ installation that I
can't.  I don't know what was used for "tm.h" and "md", nor do I know
what those refer to.  These items are supposed to be in bug reports.

I would appreciate it if you could add these details to the last
paragraph and send it to the bug list.  Thanks.

Dougm
-----

In the shell archive below is the program text and Makefile of a
program that reveals a bug in the handling of const data members.  The
removal of the line "const int bigNum = 4000;" from Integer.h leaves a
file that compiles and executes correctly.  With the line included,
however, the compile looks like:

make -k
g++ -g -I/usr/local/lib/g++-include -c  Integer.cc
g++ -g -I/usr/local/lib/g++-include -c  gcd.cc
In function struct Integer euclidGcd (struct Integer, struct Integer):
gcd.cc:11: warning: assignment of read-only location
gcd.cc:12: warning: assignment of read-only location
In function int main (int, char **):
gcd.cc:20: assignment of read-only variable `gcd'
*** Error code 1
make: Warning: Target `gcd' not remade because of errors

Compilation finished at Fri Sep 15 12:50:57

In response to a report in the gnu.g++.bugs newsgroup, I tried
compiling with the -fno-defer-pop compiler option, but this changed
nothing.

The shell archive with all the relevant source follows.  The
default.mk file is just a modification of the standard default.mk to
provide implicit C++ make rules.

The compiler is g++ version 1.35.1-.  The machine is a sun3, SunOS
release 4.0.1

Thank you for your attention to this problem.

Doug Moore (dougm@rice.edu)

#!/bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
# Integer.cc
# Integer.h
# Makefile
# gcd.cc
if test -f Integer.cc
then
echo shar: will not overwrite existing file " Integer.cc "
else
cat >Integer.cc <<'------ EOF ------'
#include "Integer.h"

Integer::Integer(const Integer& i)
     // Initialize one Integer to the value of another
{
  number = i.number;
}

Integer::Integer(int theInt)
     // A new Integer can be given a value or
     // the value can default to zero
{
  number = theInt;
}

Integer& Integer::operator%(const Integer& i)
     // divide two Integers and make the
     // remainder a new Integer
{
  Integer *rem = new Integer;
  rem->number = number % i.number;
  return *rem;
}

int Integer::operator!=(const Integer& i)
     // compare two Integers for inequality
{
  return number != i.number;
}
ostream& operator<<(ostream& os, const Integer& i)
{
  os << i.number;
  return os;
}

------ EOF ------
ls -l Integer.cc
fi # End Integer.cc
if test -f Integer.h
then
echo shar: will not overwrite existing file " Integer.h "
else
cat >Integer.h <<'------ EOF ------'
#include <stream.h>

class Integer
{
public:
  Integer(int value = 0);
  ~Integer() {;}
  Integer(const Integer&);
  Integer& operator%(const Integer&);
  int operator!=(const Integer&);
  friend ostream& operator<< (ostream& os, const Integer& num);

private:
  int number;
  const int bigNum = 4000;
};
------ EOF ------
ls -l Integer.h
fi # End Integer.h
if test -f Makefile
then
echo shar: will not overwrite existing file " Makefile "
else
cat >Makefile <<'------ EOF ------'
include /titan2/dougm/212/default.mk
C++FLAGS = -g -I/usr/local/lib/g++-include

INTEGER_OFILES = Integer.o

gcd: $(INTEGER_OFILES) gcd.o
	g++ -o gcd $(C++FLAGS) gcd.o $(INTEGER_OFILES)

$(INTEGER_OFILES): Integer.h

gcd.o: Integer.h
------ EOF ------
ls -l Makefile
fi # End Makefile
if test -f gcd.cc
then
echo shar: will not overwrite existing file " gcd.cc "
else
cat >gcd.cc <<'------ EOF ------'
#include <stream.h>
#include "Integer.h"

Integer euclidGcd(Integer a, Integer b)
     // Compute and return the greatest common divisor of a and b
     // using Euclid's algorithm
{
  while (a != Integer(0))       // a respectable Integer class would
  allow
    {                           // me to write a != 0 instead
      Integer c = b % a;
      b = a;
      a = c;
    }
  return b;
}

main(int argc, char *argv[])
     // Read several ints from the command line and print their gcd
{
  Integer gcd = 0;

  while (--argc)
    {
      // argc numbers are still to be read and gcd is the greatest
      // common divisor of the ones read so far.
      int nextInt = atoi(*++argv);
      gcd = euclidGcd(gcd, nextInt);
    }
  cout << gcd << "\n";
}
------ EOF ------
ls -l gcd.cc
fi # End gcd.cc
echo '***** End of' bug.shar '*****'


----- End Included Message -----

dougm@rhea.rice.edu (Doug Moore) (09/17/89)

I apologize for reporting as a bug what is not a bug, but a C++ language
deficiency.  I couldn't kill off the bug report in time.  Sorry.

Doug Moore (dougm@rice.edu)