πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

6 total messages Started by Jonas Mvlsd Sun, 25 Feb 1996 00:07
Calling X(int) from X()'s init list
#3438
Author: Jonas Mvlsd
Date: Sun, 25 Feb 1996 00:07
58 lines
2095 bytes
Why is it not allowed to delegate the initialisation to another 
constructor, if that constructor is *the only thing* in the 
member initialisation list? 

It seems to me that in many cases, one could avoid some code 
duplication and gain some clarity, at no conceptual or runtime cost.
The increased clarity would come from folding different constructor
cases into one case by calling the most general constructor in
the member initialisation list of the other constructors.
(I know this is not always clearer, but sometimes it is.)

Is it a case of: "Yes, it is basically a good idea, but it is too 
late in the standardisation process. Anyway, we have to draw the
line somewhere."

Or is it more like: "Of course you should not do it that way, look here..."


A small example to illustrate the technical (not the conceptual)
detatils of my question:

#include <iostream.h>

class X
{
public:
    X (int i) : i_(i) {}

    X () : X(34) {}		// Not allowed

    int i() {return i_;}

private:
    int i_;
};



int
main ()
{
    X a(55);
    X b;

    cout << a.i() << ' ' << b.i() << endl;
}
	

---
Jonas MΓΆlsΓ€, jbm@nada.kth.se

[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Calling X(int) from X()'s init list
#3495
Author: David Byrden
Date: Sun, 25 Feb 1996 16:44
32 lines
1417 bytes


<<<<<<<

Why is it not allowed to delegate the initialisation to another
constructor

class X
{
public:
    X (int i) : i_(i) {}

    X () : X(34) {}             // Not allowed
>>>>>>>>>


     Why not use      X (int i = 34 ) : i_(i) {}


                      David

[ I think he was concerned with the more general case of a complicated
  initialization (not assignment) that must be repeated for each
  constructor. -sdc, moderator
]
[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Calling X(int) from X()'s init list
#3502
Author: Anders Lindback
Date: Mon, 26 Feb 1996 16:07
37 lines
1693 bytes
In article <4gognf$et@news.bridge.net>,
David Byrden  <100101.2547@compuserve.com> wrote:
><<<<<<<
>
>Why is it not allowed to delegate the initialisation to another
>constructor
>
>class X
>{
>public:
>    X (int i) : i_(i) {}
>
>    X () : X(34) {}             // Not allowed
>>>>>>>>>>
>
>
>     Why not use      X (int i = 34 ) : i_(i) {}

>[ I think he was concerned with the more general case of a complicated
>  initialization (not assignment) that must be repeated for each
>  constructor. -sdc, moderator
>]

Well, one could use a similar thing using a member function:

X (int i) { init(i); }
X () { init(34); }

where init is a member function for class X.

Anders
[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Calling X(int) from X()'s init list
#3508
Author: Harald Fuchs
Date: Mon, 26 Feb 1996 17:23
31 lines
2116 bytes
alindbac@sw.seisy.abb.se (Anders Lindback) writes:

>> Why is it not allowed to delegate the initialisation to another
>> constructor
>> 
>> class X
>> {
>> public:
>> X (int i) : i_(i) {}
>> 
>> X () : X(34) {}             // Not allowed

> Well, one could use a similar thing using a member function:

> X (int i) { init(i); }
> X () { init(34); }

> where init is a member function for class X.

The problem is that member functions can't initialize anything.  Often
this is just a minor nuisance: the X ctor calls the default ctor of
the data member, which gets then immediately overwritten by assignment
in init().  But you can't use this technique when you deal with const
and reference members.  Thus the syntax "X () : X(34) {}" would still
be useful.  Has anyone written a proposal?
[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Calling X(int) from X()'s init list
#3524
Author: Etay Bogner
Date: Tue, 27 Feb 1996 18:41
73 lines
2990 bytes
In article <4gsa2r$gjm@sdaw04.seinf.abb.se>, alindbac@sw.seisy.abb.se wrote:

<< In article <4gognf$et@news.bridge.net>,
<< David Byrden  <100101.2547@compuserve.com> wrote:
<< ><<<<<<<
<< >
<< >Why is it not allowed to delegate the initialisation to another
<< >constructor
<< >
<< >class X
<< >{
<< >public:
<< >    X (int i) : i_(i) {}
<< >
<< >    X () : X(34) {}             // Not allowed
<< >>>>>>>>>>
<< >
<< >
<< >     Why not use      X (int i = 34 ) : i_(i) {}
<< 
<< >[ I think he was concerned with the more general case of a complicated
<< >  initialization (not assignment) that must be repeated for each
<< >  constructor. -sdc, moderator
<< >]
<< 
<< Well, one could use a similar thing using a member function:
<< 
<< X (int i) { init(i); }
<< X () { init(34); }
<< 
<< where init is a member function for class X.
<< 

Not very useful for const or reference data members ( here, useful ==
can't be done without explicitly initializing them, but than you "init"
will do only half the work )

My inclination, although I rarly use const or reference data members, will
be to wrap those data members in an internal structure with it's own
constructor :

class A_Class_With_A_Const_Or_Refs {
    A_Class_With_A_Const_Or_Refs(long& l);
    struct internal_Data {
        long& mLong;
        const long mClassID; // when there was no RTTI, for instance
        internal_Data(long& inLong, const long inID);
        } mInternalData;
    };

A_Class_With_A_Const_Or_Refs::A_Class_With_A_Const_Or_Refs(long& l) :
mInternalData(l, 'MYID') {}

and so on.

In my opinion, those cases will be very rare.

-- 
-- Etay Bogner
-- Etay_Bogner@mail.stil.scitex.com
-- Scitex Corp.
-- Israel.
-- 
-- There are two rules for success in life:                   
-- Rule 1:  Don't tell people everything you know.                
-- 
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Re: Calling X(int) from X()'s init list
#3537
Author: Etay Bogner
Date: Wed, 28 Feb 1996 16:16
51 lines
2278 bytes
In article <Etay_Bogner-2702961023450001@metay.stil.scitex.com>,
Etay_Bogner@mail.stil.scitex.com (Etay Bogner) wrote:
<< My inclination, although I rarly use const or reference data members, will
<< be to wrap those data members in an internal structure with it's own
<< constructor :
<< 
<< class A_Class_With_A_Const_Or_Refs {
<<     A_Class_With_A_Const_Or_Refs(long& l);
<<     struct internal_Data {
<<         long& mLong;
<<         const long mClassID; // when there was no RTTI, for instance
<<         internal_Data(long& inLong, const long inID);
<<         } mInternalData;
<<     };
<< 
<< A_Class_With_A_Const_Or_Refs::A_Class_With_A_Const_Or_Refs(long& l) :
<< mInternalData(l, 'MYID') {}
<< 
<< and so on.

I think it may be unclear :

my suggestion is that one can write a *lot* of constructors, and they can
call the internal constructor, which has the same effect as calling :

class X {
    X(long);
    X(char*);
    };

X::X(char* c) : X(1) {}

Which is the desired behavior.

-- 
-- Etay Bogner
-- Etay_Bogner@mail.stil.scitex.com
-- Scitex Corp.
-- Israel.
-- 
-- There are two rules for success in life:                   
-- Rule 1:  Don't tell people everything you know.                
-- 


[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
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