🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #232

default copy constructor?

#232
From: Benjamin R. Liblit
Date: Thu, 10 Sep 1992 09:09
38 lines
1447 bytes
I would like to construct an instance of a derived class using an
instance of its base class.  The plan is to use the base class's copy
constructor (a.k.a. "X(X&)") to initialize the inherited portions, and
initialize the rest explicitly.  Thus:

	struct Base
	{
	  int b;
	};

	struct Derived : public Base
	{
	  int d;
  
	  Derived(const Base &shared)
	  : Base(shared),
	    d(1)
	    {
	    }
	};

G++ 2.2.2 reports the following errors:

	try.cc: In method `Derived::Derived (const struct Base&)':
	try.cc:13: no constructor found for initialization of `Base'

I had thought that *all* structures/classes/types had a default copy
constructor defined for themselves.  This error seems to hint
otherwise.  Am I confused or is G++?

If I do explicitly define a copy constructor for the base class, the
errors disappear.  However, doing so in the real code I'm working on
is not a feasible solution.

Any ideas?
--
Ben Liblit   -----   "Fais que tes reves soient plus longs que la nuit."
liblit@cs.psu.edu   --   brl102@psuvm.psu.edu   --   brl102@psuvm.bitnet
Technical Details
Message-ID: <BuCs0n.5pF@cs.psu.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!sun-barr!ames!tulane!ukma!psuvax1!liblit

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.help
Path: arc1-ca.pugleaf.net!sparky!uunet!sun-barr!ames!tulane!ukma!psuvax1!liblit
From: liblit@cs.psu.edu (Benjamin R. Liblit)
Subject: default copy constructor?
Message-ID: <BuCs0n.5pF@cs.psu.edu>
Sender: news@cs.psu.edu (Usenet)
Nntp-Posting-Host: waldo.cs.psu.edu
Distribution: gnu
Date: Thu, 10 Sep 1992 09:09:54 GMT
Lines: 38