Re: Default constructors, passing argument

From: Peter (peter_at_peter.com)
Date: 07/09/04


Date: Fri, 9 Jul 2004 01:03:04 +0000 (UTC)

On Fri, 09 Jul 2004 00:24:03 GMT, Chad J McQuinn wrote:

> In article <1p8c7eavfj6vc$.15zqrv58cnm0k.dlg@40tude.net>,
> Peter <peter@peter.com> wrote:
>
>> class MySubClass : MyClass {
>> public:
>> int val; //public member of class for a good reason
>>
>> MySubClass() : MyClass("a string", 25) {
>> Func(); //function that needs to use val
>> }
>> }
>>
>> class Thing {
>> public:
>> MySubClass hello; //public because I was told to in this thread
>>
>> Thing(int val) : hello(val) {
>>
>> }
>> }
>>
>> As you can see val is no longer an attribute of class Thing, because val is
>> in reality an attribute of Thing placed in MySubClass to take advantage of
>> important functions there. MySubClass compiles ok. Thing does not
>> compile, but if I change MySubClass to take an int argument in its
>> constructor then the compiler says it does not have "an appropriate default
>> constructor", if I leave it as above there are more error messages.
>
> The "hello(val)" will call whatever constructor of MySubClass is capable
> of taking an integer; here there is none. Add one and your example
> should be fine.
>
> MySobClass(int myVal) : MyClass(...whatever...) , val(myVal)
> {
> Func();
> }
>
> If, as you say in your comment, Func needs to use val, then you should
> remove the call to Func from the default constructor entirely. Or, if
> you never want MySubClass constructed without passing an int to the
> constructor, eliminate the default constructor entirely. If doing so
> gives you an error, then that means you are creating a MySubClass
> somewhere and not passing in an int.
>
> -Chad

It leaves me with the same problem. The default constructor never calls
the constructor with the all important function in it. If something is a
member object of a class, its default constructor gets called. The
initialiser list seems to have had no effect at all.

class MySubClass : MyClass {
  public:
    int val; //public member of class for a good reason

    MySubClass() : MyClass("a string", 25) {
      //Func();
    }

    MySubClass(int val) {
      MySubClass::val = val;
      Func(); //function that needs to use val
    }
}

class Thing {
  public:
    MySubClass hello; //public because I was told to in this thread

    Thing(int val) : hello(val) {
      
    }
}

I know it was optimistic to expect it to call 2 constructors: when this
program executes it the hello constructor misses out the Func() because the
default constructor is used when declaring it. The

hello(val)

has no effect. If I make it so the first constructor takes an int and
calls func and remove the default constructor then the compiler says "no
default constructor available".

Whichever way val is not given its value or func does not get called.



Relevant Pages

  • Re: Default constructors, passing argument
    ... MySubClass compiles ok. ... but if I change MySubClass to take an int argument in its ... > constructor", if I leave it as above there are more error messages. ... If, as you say in your comment, Func needs to use val, then you should ...
    (microsoft.public.vc.mfc)
  • Re: Default constructors, passing argument
    ... >> I have a subclass derived from another class as a member of my class. ... >> has a default constructor with the initialiser list used to pass values ... at the same time as Thing is constructed I need MySubClass to get the ... > If, as you say in your comment, Func needs to use val, then you should ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Augmenting Types
    ... On 1/4/10 12:02 AM, Thomas 'PointedEars' Lahn wrote: ... Augmenting Types, e.g. ... Function.prototype.method = function{ ... none of the built in functions may be used as a constructor. ...
    (comp.lang.javascript)
  • Re: Augmenting Types
    ... kangax wrote: ... Augmenting Types, e.g. ... Function.prototype.method = function{ ... none of the built in functions may be used as a constructor. ...
    (comp.lang.javascript)
  • Re: Problem with linker
    ... but to have actually written a default constructor. ... such as overloading on int and pointer types. ... conversion of 0 to CString requires a user-defined conversion, ... acceleration operator *(distance d, time_squared t2); ...
    (microsoft.public.vc.mfc)