Re: Default constructors, passing argument

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


Date: Fri, 9 Jul 2004 00:33:21 +0000 (UTC)

On Fri, 9 Jul 2004 00:00:04 +0000 (UTC), Peter wrote:

> On Thu, 08 Jul 2004 20:15:27 GMT, Chad J McQuinn wrote:
>
>> In article <6fdhqkfba6m8$.143lq7694c4m4$.dlg@40tude.net>,
>> Peter <peter@peter.com> wrote:
>>
>>> class Thing {
>>> private:
>>> int val;
>>> MySubClassObject hello; //constructed but needs val
>>>
>>> public:
>>> Thing(int value) {
>>> val = value; //value got too late
>>> }
>>> };
>>>
>>> What are the options?
>>
>> Thing(int value) : val(value), hello(value)
>> {}
>>
>> The part after the colon in Thing's constructor is called the
>> initialization list. You can use it to initialize member variables as
>> well as to call base class constructors of class Thing (if there were
>> any).
>>
>> It looks like in this simple example, you might have created the 'val'
>> member variable just construct 'hello'. If so that is nether possible
>> nor necessary.
>>
>> -Chad
>
> thanks for the replies, but there is a problem
>
> I have made changes like this now:
>
>
> 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.
>
> ANGST FOLLOWS
>
> What is
>
> hello(val)
>
> doing to clarify? Is it calling a constructor of MySubClass that takes an
> int? How do I get a value into val for hello as Thing enters the
> constructor? I do not want hello constructed without its val argument.
>
> Prior to asking this, my workaround was to have a MySubClass* as an
> attribute and construct it after receiving val ie
>
> //val is passed in and construction of hello takes place only when value
> //is there
> hello = new MySubClass(val);
>
> I do not want the pointer solution above because despite creating and
> deleting appropriately somewhere along the line it provoked an access
> violation. I also have learned to hate pointers with class wide scope due
> to some nasty experiences recently with access violations or maybe I am
> blaming them on the wrong thing...it is hard to say but there seems to be
> fewer problems without messing around with pointers.
>
> I need more explanations on this, if people will be so kind, to understand
> more.
>
> Thanks.

Sorry about messing up the follow up...grrr



Relevant Pages

  • Re: Default constructors, passing argument
    ... The part after the colon in Thing's constructor is called the ... You can use it to initialize member variables as ... well as to call base class constructors of class Thing (if there were ...
    (alt.comp.lang.learn.c-cpp)
  • 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
    ... 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
    ... 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)