Re: Default constructors, passing argument

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Chad J McQuinn (huskerchad_at_removethis.andthistoo.insightbb.com)
Date: 07/09/04


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



Relevant Pages

  • Re: Default constructors, passing argument
    ... class MySubClass: MyClass { ... MySubClass compiles ok. ... constructor then the compiler says it does not have "an appropriate default ... I also have learned to hate pointers with class wide scope due ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Abstract Base Constructor Question
    ... In myBase, the only constructor you have takes a single argument. ... mySubClass, the only constructor takes no arguments. ... you can specify which base class constructor to call and do ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: Default constructors, passing argument
    ... MySubClass compiles ok. ... but if I change MySubClass to take an int argument in its ... > 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. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Default constructors, passing argument
    ... You can use it to initialize member variables as ... MySubClass compiles ok. ... > constructor then the compiler says it does not have "an appropriate default ...
    (alt.comp.lang.learn.c-cpp)