🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Paul Johnson Fri, 17 Nov 1995 17:22
explicit call of constructor
#2624
Author: 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. ]
Re: explicit call of constructor
#2630
Author: Steve Clamage
Date: Sat, 18 Nov 1995 03:49
30 lines
1396 bytes
Paul Johnson <pjcj@transeda.demon.co.uk> writes:

>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.

You are correct. It is invalid to attempt to call a constructor
directly (or to take the address of a constructor).

Evidently g++ allows calling a constructor as an extension.

You can usually get the effect of calling a constructor by
using placement-new. That is,
given:
	T* t = ...;
instead of:
	t->T(); // illegal
write:
	new (t) T(); // placement-new
--
Steve Clamage, stephen.clamage@eng.sun.com


---
[ 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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads