|
-
March 1st, 2002, 01:20 PM
#1
base class ptrs as data members
Hello All,
I have a class that has a data member which is an array of pointers to derived class instances.
ex.
class COwner
{
public:
CProperty *array[28];
int num_props;
public:
COwner();
~COwner();
SetProp(int num_props);
virtual bool buyProperty(CProperty* const) = 0;
virtual CProperty* sellProperty(const CPoint&) = 0;
const CProperty* operator[](int n) const;
};
CProperty is a base class, the derived classes are CHouses, CApartments, CBeachfronts.
Question: (hope this isn't too trivial)
1. do I assign the array of pointers to the derived instances in the constructor of "COwner" class?
I am using MFC application. there is no main!
any help is appreciated.
thank you
-
March 3rd, 2002, 04:50 AM
#2
Re: base class ptrs as data members
Firstly, as a small point, use std::vector rather than an array.
Are you always going to have exactly 28 of them? (You seem to be indicating that an owner has exactly 28 properties. )
You should also make your destructor virtual.
You should also be aware that if you use std::vector the pointers themselves will not automatically be deleted.
Also, you may want to make the vector protected - your derived Owner classes would then have full access but external classes would have to use the [] operator.
Also, why does your [] operator return const? Is that for a good reason - it means that external classes can only call const methods of the property. The const at the end looks fine - as the overload does not modify any members of COwner.
As for how you get the pointers, it is likely to depend on the derivation of COwner (which is an abstract base class).
You haven't said whether CProperty itself is also abstract, but I will assume so.
Now to answer your question though, from where will your constructor get the information about initializing the pointers (or are they all NULL)?
If you use a vector, you can automatically initialize them to NULL, even with a size of 28 (if you really insist). Thus:
protected:
std::vector < CProperty * > m_propArray;
// later on
COwner::COwner()
: m_propArray( 28, NULL )
{
// any other initialization
}
With your current layout, there is no way to initialize them all to NULL in the initializer list, so you would have to do it in the body of the constructor.
If you have derived instances of CProperty, and they are passed in as a vector, you could "copy-construct" the vector, thus:
COwner::COwner( const std::vector<CProperty*> & propArray )
: m_propArray( propArray )
{
// any other initialization
}
Once again if you use a regular array and get a CProperty** const parameter (or CProperty *& const), you will have to copy them inside the body of your constructor.
The best things come to those who rate
-
March 4th, 2002, 04:08 PM
#3
Re: base class ptrs as data members
Thanks for responding to my post.
I understand all of your comments about my post and I appreciate your input greatly.
All of which are no problem to change. i.e. vectors vs. arrays, virtual destructor, protected vs. public, return const.
CProperty is an abstract class. COwner is an abstract class also. The 28 elements is the max number of property a person can own.
I'm not quite sure I understand your last code snippet.
But here is what I want to do:
create an derived instance:
assign it to base class pointer:
CApartment townhouse; // derived from CProperty
array[0] = &townhouse; // base class array of pointers
array[1] = &townhouse1;
array[2] = &storefront;
I've tested this in a function that I created, but it's only good in that function. I want all or any function to be able to work with the array of pointers, without using "main"...this is an MFC app.
an example using the above would probably be much clearer for me to understand.
I'd appreciate any insight to my post.
thanks again.
-
March 4th, 2002, 06:34 PM
#4
Re: base class ptrs as data members
When you are storing pointers, you have to be careful about the lifetime of the pointer.
If they are allocated with new, they need to remain as long as all the references (pointer instances) are required to be valid, and no longer than that of the last reference. Therefore you might want to consider keeping a vector of "smart" pointers. These are not actually pointers at all, but are objects that contain a pointer.
There are different types of smart pointer. Boost offer various different ones in their libraries (at http://www.boost.org) and I have also written some (which I will post again on this site because I have changed it again).
The best things come to those who rate
-
March 4th, 2002, 08:29 PM
#5
Re: base class ptrs as data members
Thanks for the info.
I appreciate your help.
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
|