home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- From: nikki@trmphrst.demon.co.uk (Nikki Locke)
- Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
- Subject: Re: Help with nested class and constructor
- Reply-To: nikki@trmphrst.demon.co.uk
- Distribution: world
- X-Mailer: cppnews $Revision: 1.20 $
- Organization: Trumphurst Ltd.
- Lines: 66
- Date: Fri, 20 Nov 1992 17:33:30 +0000
- Message-ID: <722306010snx@trmphrst.demon.co.uk>
- Sender: usenet@gate.demon.co.uk
-
- In article <Bxyxrv.EJ3@news.cso.uiuc.edu> macg9505@uxa.cso.uiuc.edu (Michael Alan Corn) writes:
- > I could use a little help on nested classes:
- > I would like to include one of Borlands Array classes in another class,
- > in the private data section, but since the constructor requires an int to
- > initialize the class, I'm not sure how or where to give it the value:
- >
- > class foo {
- > private:
- > Array myarray(10); ///this doesn't work, the compiler chokes
- You want ...
- class foo {
- private:
- Array myarray;
-
- And in you constructor write ...
-
- foo::foo(/* arguments, if any */)
- : myarray(10) // Other initialisers
- {
- // Code, if any
- }
-
- > so I've tried this, which seems to work
- >
- > class foo {
- > private:
- > void *myarray;
- >
- > and then in the constructor for foo I do the following:
- >
- > foo::foo()
- > {
- > Array temp(10);
- > myarray = &temp;
- Oops - your void pointer points to a local variable on the stack. This
- pointer will become invalid as soon as you return from the constructor.
-
- > Fine, the compiler seems to like this, but now I can't figure out how to
- > access the member functions of the object I store in myarray. I use the Array
- > function ptrAt to get a pointer to the object:
- >
- > myarray->ptrAt(index) and then, since ptrAt returns a Object * I do the
- > following to get at that objects member function
- > myarray->ptrAt(index)->memfunc(); This doesn't work. The compiler complains
- > about every "->"; I assume that this is a precedence problem, but I really
- > don't know. Any help would be greatly appreciated. Thanks
- > macg9505@uxa.cso.uiuc.edu
- But you have declared myarray as a void pointer. A void pointer is not an
- Array pointer.
-
- I think you would gain a lot from reading the C++ Frequently Asked
- Questions list.
-
- Many thanks to Marshall Cline for compiling and maintaining it.
-
- It can be obtained ...
-
- Via ftp - Anonymous ftp from sun.soe.clarkson.edu [128.153.12.3] :pub/C++/FAQ
-
- Via mail - send mail
- To: archive-server@sun.soe.clarkson.edu
- Subject: send C++/FAQ
-
- --
- Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
- trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
-