Re: Default constructors, passing argument
From: Peter (peter_at_peter.com)
Date: 07/09/04
- Next message: Uwe Mayer: "IO-Operator overloading question"
- Previous message: Barry Schwarz: "Re: passing pointers [C]"
- In reply to: Chad J McQuinn: "Re: Default constructors, passing argument"
- Next in thread: Peter: "Re: Default constructors, passing argument"
- Reply: Peter: "Re: Default constructors, passing argument"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 9 Jul 2004 00:00:04 +0000 (UTC)
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.
- Next message: Uwe Mayer: "IO-Operator overloading question"
- Previous message: Barry Schwarz: "Re: passing pointers [C]"
- In reply to: Chad J McQuinn: "Re: Default constructors, passing argument"
- Next in thread: Peter: "Re: Default constructors, passing argument"
- Reply: Peter: "Re: Default constructors, passing argument"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|