Re: Default constructors, passing argument
From: Peter (peter_at_peter.com)
Date: 07/09/04
- Next message: Peter: "Re: Default constructors, passing argument"
- Previous message: Peter: "Re: Default constructors, passing argument"
- Maybe in reply to: Peter: "Default constructors, passing argument"
- Next in thread: Chad J McQuinn: "Re: Default constructors, passing argument"
- Reply: Chad J McQuinn: "Re: Default constructors, passing argument"
- Reply: Ben Cottrell: "Re: Default constructors, passing argument"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Peter: "Re: Default constructors, passing argument"
- Previous message: Peter: "Re: Default constructors, passing argument"
- Maybe in reply to: Peter: "Default constructors, passing argument"
- Next in thread: Chad J McQuinn: "Re: Default constructors, passing argument"
- Reply: Chad J McQuinn: "Re: Default constructors, passing argument"
- Reply: Ben Cottrell: "Re: Default constructors, passing argument"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|