C++ class question

BillLeeLee

[H]F Junkie
Joined
Jul 2, 2003
Messages
13,486
Hey all,

I'm a little rusty on my C++ classes, and I have what I think is a pretty basic question.

In my computer graphics textbook, there is sample code given to draw a Sierpinski Gasket.

Code:
class GLintPoint {
    public:
        GLint x,y;
}

........

void Sierpinski( void )  {
    GLintPoint T[3] = { {10,10}, {300,30}, {200,300} };
    int index = random(3);
    GLintPoint point = T[index];
    drawDot(point.x, point.y);
    for(int i = 0; i < 8000; ++i) {
        index = random(3);
        point.x = (point.x + T[index].x) / 2;
        point.y = (point.y + T[index].y) / 2;
        drawDot(point.x,point.y);
    }

    glFlush();
}

My question concerns the line:

Code:
GLintPoint T[3] = { {10,10}, {300,30}, {200,300} };

I know it makes an array of 3 GLintPoint objects, but how does the class know to put a value for x, and a value for y? The class definition doesn't have constructors or anything (and the default constructor is just something like GLintPoint::GLintPoint()). The class is everything that's declared - that is, just with two variables: x and y of type GLint.

And yes, the code works. I had to implement other parts of it, but the code compiles and works correctly.

If anyone could explain what happens with this, I'd be very grateful. :)
 
The class is implemented as if it's a structure. You're welcome to initialize structures with initializer lists:

Code:
struct myPoint
{
int x, y;
};

struct myPoint = {3, 2};

so an array of structures (or classes -- collectivey "aggregate types") can be initialized in the same way. The values are taken in the order they're declared in the struct.

See Section 5.7 of "The C++ Programming Language", 3rd edition. Section 8.5.1 of the C++ Standard deals with initialization of aggregate tpyes.
 
You GLintPoint is what's known as a POD (Plain Old Data) class, which can be initialized just like a C struct. POD classes have no user-defined ctor or dtor, no copy or assignment operator, no virtual methods, no private or protected nonstatic members, or members that are references. They also cannot be derived from any other class.

Another way to think about it is in C++ class and struct are really the same thing, just with different default member access. Classes by default have private access, and structs by default are public. Your simple class with all public members is just like a struct, and can be initialized like one.
 
Thank you both, mikeblas and bassman. I never saw that in Prata's C++ Primer Plus and I haven't read much of the C++ Programming Language yet. :)
 
BillLeeLee said:
Thank you both, mikeblas and bassman. I never saw that in Prata's C++ Primer Plus and I haven't read much of the C++ Programming Language yet. :)

No worries. bassman is right about there not being a user-defined constructor, BTW. The first pagaragraph of the reference I gave to the standard explains this.

You might want to fix that code for perf: the array of objects is constructed and never changed. But because of the way its declared, the objects are created and initialized every time the function is called.

This change

Code:
void Sierpinski( void )  {
    static const GLintPoint T[3] = { {10,10}, {300,30}, {200,300} };
    // ...

should get the declaration in better shape. Then, you can turn your attention to figuring out why the values are copied to a point before they're used.
 
Back
Top