|
-
March 18th, 2011, 11:34 AM
#1
-
March 18th, 2011, 11:37 AM
#2
Re: Simple classes question
What do you think pA is pointing to in your B instance?
-
March 18th, 2011, 11:44 AM
#3
Re: Simple classes question
I change it into reference but still it doesn't work
-
March 18th, 2011, 11:45 AM
#4
Re: Simple classes question
The use of whitespace would make your code more readable.
-
March 18th, 2011, 11:51 AM
#5
Re: Simple classes question
 Originally Posted by Lucassergei
I change it into reference but still it doesn't work 
I wouldn't expect it to. References and pointers refer or point to other objects.
Why not just make it an instance?
And I agree, please get in the habit of using spaces.
-
March 18th, 2011, 12:05 PM
#6
Re: Simple classes question
I agree with GCDEF, it would be much easier to just make it an instance. If you insist on using pointers, you'll have to allocate them. You'll do this in your constructors (which you have neglected to implement). You'll also have to deallocate these pointers, which you'll do in your destructors (which you also must implement).
-
March 18th, 2011, 04:40 PM
#7
Re: Simple classes question
Either it needs to be an actual object, or you need to direct the pointer at some existing object before trying to use it.
-
March 19th, 2011, 06:48 AM
#8
Re: Simple classes question
You don't need typedef keyword in defining structs in C++. You have pointers to A and B but never have those been initialized before deferencing them. Of course, it will lead to a crash.
Also, if I may comment on the purpose being served by your code. You have a structure which is being operated upon by B (which only sets one of the members) and C (which sets the other member, and with extra level of nesting i.e. via a pointer to an object of type B, and B is just setting the member directly of that of object of A). What purpose does it serve? Why not have just one class A and have the parameterized constructors that setup values for both members of it. But may be you have a bigger purpose than this but if that bigger purpose does try achieve the same as you have depicted by the above sample code, it seems very inelegant.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|