tags:

views:

45

answers:

2

I have the following class:

GLRectangle.h

#include "XPView.h"

class GLRectangle
{
public:
    int top, left, bottom, right;

public:
    GLRectangle(void);
    ~GLRectangle(void);
    GLRectangle* centerRect(int rectWidth, int rectHeight, int boundWidth=0, int boundHeight=0);
};

GLRectangle.cpp

#include "GLRectangle.h"

GLRectangle::GLRectangle(void)
{
}

GLRectangle::~GLRectangle(void)
{
}

GLRectangle* GLRectangle::centerRect(int rectWidth, int rectHeight, int boundWidth, int boundHeight)
{
    if(boundWidth == 0)
    {
        boundWidth = XPView::getWindowWidth();
    }

    if(boundHeight == 0)
    {
        boundHeight = XPView::getWindowHeight();
    }

    // Set rectangle attributes
    left   = boundWidth / 2 - rectWidth / 2;
    top    = boundHeight / 2 + rectHeight / 2;
    right  = boundWidth / 2 + rectWidth / 2;
    bottom = boundHeight / 2- rectHeight / 2;

    return this;
}

and I'm trying to chain a function onto the construction of the object as follows:

wndRect = new GLRectangle()->centerRect(400, 160);

but getting the following error:

error C2143: syntax error:missing ';' before '->'

Is there a way to get this to work?

+1  A: 
(wndRect = new GLRectangle())->centerRect(400, 160);

but why do this? Why not provide parameters to your constructor so you can say:

wndRect = new GLRectangle( 400, 160 );
anon
I think you might have misplaced you first parenthesis
Gab Royer
The constructor will also work, but I want to keep the code self documenting and self explanatory if I can. Maybe I should consider: wndRect = (new GLRectangle(400, 160))->center(); // Mmmm :)
Joe Zephyr
@Gab Why do you think that?
anon
Never mind, you're saving yourself an assignment operator call like this ;)
Gab Royer
+2  A: 

It's an operator precedence problem. Try

// Add some brackets
wndRect = (new GLRectangle())->centerRect(400, 160);
Jeff Foster
Thanks, that fixed it!
Joe Zephyr