octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: default constructor and class


From: Patrick Noffke
Subject: Re: default constructor and class
Date: Mon, 8 Jul 2013 14:53:23 -0500

On Mon, Jul 8, 2013 at 2:42 PM, Michael Goffioul
<address@hidden> wrote:
>
> On Mon, Jul 8, 2013 at 3:34 PM, Marco Vassallo <address@hidden>
> wrote:
>>
>> ________________________________
>> > Date: Mon, 8 Jul 2013 15:27:45 -0400
>> > Subject: Re: default constructor and class
>> > From: address@hidden
>> > To: address@hidden
>> > CC: address@hidden
>> >
>> > On Mon, Jul 8, 2013 at 3:20 PM, Marco Vassallo
>> > <address@hidden<mailto:address@hidden>> wrote:
>> > Hi all,
>> >
>> > for the fem-fenics pkg I'm creating a new class, and I thus add as
>> > private members
>> >
>> >    DECLARE_OCTAVE_ALLOCATOR;
>> >    DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA;
>> >
>> > and also the DEFINE counterpart,
>> >
>> > which requires the class to have a default constructor (right?)
>> >
>> > The problem is that one of the parent class from which I derive my own
>> > class
>> > doesn't provide a default constructor. Example
>> >
>> > class B: public octave_base_value, public A
>> > {
>> >    B(): octave_base_value(), A(), B() {} //error: A() not available
>> >
>> >    private:
>> >    A my_element;
>> >    DECLARE_OCTAVE_ALLOCATOR;
>> >    DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA;
>> > }
>> >
>> > How can I manage it?
>> >
>> > I'm not sure I understand your construct correctly, but is it
>> > intentional that B inherits from A *and* contains a A object
>> > (my_element)? Can't you get rid of the inheritance from A and just have
>> > the my_element member?
>> >
>> > Michael.
>>
>> This class should be a wrapper for the A class.
>> Even if I avoid deriving from A,
>>
>>  class B: public octave_base_value
>> {
>>  B(): octave_base_value() {} //error: default constructor for my_element
>> not available
>>
>>  private:
>>  A my_element;
>>  DECLARE_OCTAVE_ALLOCATOR;
>>  DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA;
>>  }
>>
>> I still need a constructor for my_elements, and as it is an object of type
>> A I
>> need to initialize it somehow.
>
>
> Why don't you do the following instead for the default constructor of class
> B:
>
> B(): octave_base_value(), my_element(<whatever_argument>) {}
>
> Michael.
>

If the default constructor for A was made private, there is probably
another constructor defined, otherwise you could never instantiate A.
So, as Michael suggested, you need to pass the required arguments to
the appropriate constructor for A.  If that means B needs the same
parameters in its constructor, then you should probably also make B
have a private default constructor (unless you can come up with
sensible defaults for A in your B's default constructor).

Is 'A' an existing class within Octave that you are using, or another
class you wrote?

Patrick


reply via email to

[Prev in Thread] Current Thread [Next in Thread]