Re: Default constructors, passing argument
From: Chad J McQuinn (huskerchad_at_removethis.andthistoo.insightbb.com)
Date: 07/09/04
- Next message: Joseph M. Newcomer: "Re: multiple view single document"
- Previous message: Doug Harrison [MVP]: "Re: sizeof() ?"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 09 Jul 2004 00:24:03 GMT
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
- Next message: Joseph M. Newcomer: "Re: multiple view single document"
- Previous message: Doug Harrison [MVP]: "Re: sizeof() ?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|