0

I have a class called RenderObject that holds 3 variables: a position, an int that holds the length of an array pointed to, and the pointer to an array of "Faces"

Face is just a class that holds its type (quad or triangle), its colour, and a pointer to an array of vertices (stored as Vector3f).

My RenderObject class has a constructor that requires a position, an array of faces, and and int that is the number of faces in the array.

I also have a class called Cube, it is derived from RenderObject (so I could easily generate a cube of width, height and depth dimensions.

The problem is, Cube's constructor only needs the position and dimensions, so when it comes to calling the default constructor:

Cube::Cube( sf::Vector3f *positionVector, float width, float height, float depth ) : RenderObject( positionVector, /*can't supply this*/, 6 ) {

I can't pass the array of faces because the Cube class runs GenerateFaces(); to automatically create the faces according to the dimensions.

So my question is, how do I pass the generated faces to RenderObject's constructor? Or is there a better way to do this?

4

2 回答 2

2

There are a couple of ways to solve this. Some of the most common are to either make the array of faces protected so it can be accessed by the child classes, make a protected function SetFaces in the base class to set the array, or make the GenerateFaces a method in the base class that can access the private variables.

Oh and a last tip: Don't use raw arrays/pointers, use std::vector instead.

于 2012-12-23T07:15:49.833 回答
1

Make the method GenerateFaces a static method. You can then call it from within the base-constructor call.

I'm assuming here that the GenerateFaces method can do it's work with just the parameters from the Cube-constructor. You will have to add these as parameters to the GenerateFaces method.

Edit: just saw the C++ tag while I had C# in mind. Should still be applicable though.

于 2012-12-23T07:11:57.127 回答