Archived

This topic is now archived and is closed to further replies.

Composition problem

This topic is 6924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello everyone, I have a question about Composition. I want to have a class called Points inside class called Poly. Now at design time I don’t know how many points Poly will have so I want to make a pointer to Points and at the same time pass a constructor. Here is a example: class Poly { public : Poly(int px, int py, int amount, float 0x,0y) : Points(0x,0y); private : Points *ThePoints; int amount, x, y; //Amount is the amout of // points poly will have }; class Points { public : Points(float, float); ~Points(); private : float x, y; }; Now my question is, when do I allocate the memory for Points? I would think I allocate it in the constructor of Poly but the constructor for Points would be called before Poly if i'm correct? So how can it be called if there is no memory allocated for it? If someone can help, I thank you a great deal because I can't figure out where to put ThePoints new Points[amount]; Thanks again braves Edited by - braves on 5/2/00 12:36:46 PM

Share on other sites
Hi
At first this will be NO real answer to your question. I not
very good in C++ so please corect me if I''m wrong.(I have just read one book and not much experince). But I think it have to be:
ThePoints = new Points[amount];

And what is px and py in Poly if it is suposed to be the position I think you don''t need it, because the Poly positition is in a way give by the positions of the point the Poly is made of.
Then I wanted to add that if I understood it right you give with 0x and 0y the cordinats of ONE point, so you create a Poly with one defined Point how are you define the other Points, with a function of Points? If yes, why don''t you first Construct a Poly with x points and then define the points with an other function:
BOOL DefPoints(int *Points);//Points is an array of Points Cordinats

Now a last one question, isn''t that:

class a {
public:
a() : b();
~a();
};

class b {
b();
...
};
just used to call when b is parent class of a and then only written in the implementation?

Share on other sites
Aragorn,
I just added px and py just so I have something to the constructor. I wanted more parameters than the ones being passed too the Point class(this is not what my class really looks like, It’s just an example) I was thinking about adding another member function that defines the points like you said and was planning on doing it. Than curiosity came in and I wanted to know how to do it, just to know. So I hope someone can help me.
braves

Share on other sites
quote:

I would think I allocate it in the constructor of Poly but the constructor for Points would be called before Poly if i'm correct? So how can it be called if there is no memory allocated for it?

Remember, to the compiler your class is not composed of Points anymore. It is composed of a pointer to Points, the size of which is already known. No matter whether this poly uses 1 Point or 100 Points, your actual Poly class is the same size.

This also poses you another problem: you can initialise members in a constructor, but you don't have a member which is of type Point, just a pointer to one. Therefore using the member initialiser syntax to initialise a Point is not gonna work.

And which Point were you hoping to initialise with 0x, 0y (invalid identifiers, surely?) All of the points in your array? The first one only? You need to give this some more thought.

I might use a constructor similar to this:
Poly :: Poly(int a) : amount(a){    ThePoints = new Points[amount];}

Initialising the points will be more tricky. I don't think you'd want to get into variable length parameter lists, so you'll need some way of accessing individual Points later. Or change your design so that a Poly starts off empty and you add points as you go along, with a function like Poly::AddPoint(Point& newPt).

Edited by - Kylotan on 5/3/00 5:04:50 AM

• 10
• 13
• 52
• 11
• 15