🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2624

explicit call of constructor

#2624
From: Paul Johnson
Date: Fri, 17 Nov 1995 17:22
75 lines
2613 bytes
I am porting some code that was developed using g++ 2.7.0 on multiple
platforms, and have come across several explicit calls of
constructors.  My reading of the ARM and the latest draft to which I
have access (28th April 95) suggests that this is not correct
behaviour.

I have created a little program to demonstrate the problem:

-----------------------------------------------------------------------

#include <stdio.h>

class c
{
  int i;
public:
  c(int n) { printf("in c::c() for %d at %p\n",i=n,this); }
  c() { printf("in c::c(%d) at %p\n",i,this); }
  ~c() { printf("in c::~c(%d) at %p\n",i,this); }
};

int main()
{
  c c1(1), c2(2);
  c *cp=&c2;
  c1.c();
  cp->c();
  return 0;
}

-----------------------------------------------------------------------

g++ compiles this without even a warning and produces the following
output:

in c::c() for 1 at effffdf4
in c::c() for 2 at effffdf0
in c::c(1) at effffdf4
in c::c(2) at effffdf0
in c::~c(2) at effffdf0
in c::~c(1) at effffdf4


Sun CC moans:

error: illegal function call: explicit call of constructor c ()

for the two appropriate lines, but it is RELEASE SC1.0   1Mar1991


Visual C++ version 2.0 on NT version 3.51 chokes with something like:

function c is not a member of c

or something similar - I don't have access to it right now.


So, who is correct?  I'd plump for g++ having a bug, but I thought I'd
better check to ensure that the standard had not been changed, or that I
had missed something in the documents.

The ARM states in 12.1:
    ... A constructor differs from all other nonstatic member functions
    in that it is _not_ called for an object of its class ...
but I could not find anything quite so explicit in the newer documents.

Can anyone be definitive on this matter?

-- 
Paul Johnson - pjcj@transeda.demon.co.uk
TransEDA Ltd, Chandlers Ford, Hants. UK.

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <48if4j$9eb@brian.transeda.demon.co.uk>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: Paul Johnson <pjcj@transeda.demon.co.uk>
Newsgroups: comp.std.c++
Subject: explicit call of constructor
Date: 17 Nov 1995 17:22:48 GMT
Organization: TransEDA Ltd, Chandlers Ford, Hants. UK.
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <48if4j$9eb@brian.transeda.demon.co.uk>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
Content-Length: 1853
X-Lines: 71
Originator: clamage@taumet