3

Possible Duplicate:
When do I use a dot, arrow, or double colon to refer to members of a class in C++?

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

In the code above, what does -> mean? Thanks.

Community
  • 1
  • 1
  • 5
    That's a strangely basic question to ask from the middle of some advanced code. How did you get this far? I'd recommend reading a good book on C++ for a bit until you're familiar with all the fundamentals. – Kerrek SB Nov 14 '11 at 18:04
  • 1
    @KerrekSB, my guess is it's either an example or template project of IDE. – Michael Krelin - hacker Nov 14 '11 at 18:05
  • 2
    @Nanocom: When you put a -1 on this, Did you tell the OP what should be searched for or what book should be referred? or even pointed him/her in the right direction? Just an overzealous -1 helps no one. – Alok Save Nov 14 '11 at 18:10
  • 1
    This is pretty much basic and You need to pick up a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn it. – Alok Save Nov 14 '11 at 18:11
  • 1
    What... no one here has worked with Qt and Qt's ide (Qt Creator)? This is the basic 'template' for a Qt Gui app. – g19fanatic Nov 14 '11 at 18:18
  • 1
    Why the hell are people downvoting this? A smug sense of superiority? It's a legitimate question, not poorly worded and polite. The only reason I can think of is that the question may be a duplicate, but there is a close vote feature for that very reason. – Tamás Szelei Nov 14 '11 at 18:23
  • I downvoted it because a google search for "C++ operators" led to me wikipedia which gave a high level description of the operator. By using its suggested name I immediately found http://www.cplusplus.com/doc/tutorial/structures/ which seems to show its use nicely, showing a lack of searching as well as the fact that it's a duplicate. – Mark B Nov 14 '11 at 18:31
  • @MarkB, StackOverflow aims to collect the answers for questions, not to send away beginners yelling RTFM at them. He might not even know what to search for! – Tamás Szelei Nov 14 '11 at 18:34
  • @Als: everybody can do a google research... – Nanocom Nov 14 '11 at 19:06
  • 1
    @Nanocom: If they know what to search for! – Alok Save Nov 14 '11 at 19:07
  • All I can say is that How to think like a computer scientist c++ edition really isn't a good book to be reading by itself. –  Nov 14 '11 at 20:18

6 Answers6

7

It means the object on the left side of the arrow is a pointer to an instance of a class or structure, and the name on the right is a member of that class or structure.

In the context:

ui->setupUI(this);

it means that ui is a pointer to an instance of the class Ui::MainWindow and the setupUI(this) is an invocation of the setupUI member function of that class with the parameter this.

When you have an actual instance of a class (or structure) instead of a pointer, then you use the notation:

Ui::MainWindow non_ptr; // not a pointer
non_ptr.setupUI(this);

Strictly, you can also write:

(*ui).setupUI(this);

However, you will rightly be ostracized if you write code like that rather than using the -> arrow operator.

Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

In most cases, x->y is the same as (*x).y. Things like iterators can overload -> to do a bit of bonus stuff but in general it's a shorthand.

Toomai
  • 3,974
  • 1
  • 20
  • 22
2

ui-> is equivalent to (*ui). (access the member of the object pointed to by ui).

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
2

ui->setupUi(this) is actually a short way of writing (*ui).setupUi(this).

P.S.: Somebody had downvoted your question, so I upvoted it. To the downvoter: come on! So what if this is a trivial question? It might be a perfectly reasonable one for a person who is just starting to learn C++.

  • 2
    I did not downvote.But come on now.Who starts to learn `C++` with code like `MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)`?This is very scary for a beginner.... – Cratylus Nov 14 '11 at 18:12
  • True that. But we don't know his situation. Maybe it just so happened that he's stuck with some Qt code that he needs to fix real quick. –  Nov 14 '11 at 18:20
  • 2
    `MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)` is exactly what will be generated by IDE on creation of a new Qt GUI application - probably the simplest starting point for a new user of Qt. – sam-w Nov 14 '11 at 18:27
  • That's auto-generated code that QtCreator generates. – Tamás Szelei Nov 14 '11 at 18:32
1

It is a notation in both C and C++ to indicate that we access a variable via a pointer

Cratylus
  • 52,998
  • 69
  • 209
  • 339
1

Assuming ui is a pointer type (as implied by initializing it with new UI::MainWindow), then ui->setupUi is equivalent to *(ui).setUi.

However, it's also possible to overload operator-> for a class, in which case it's basically equivalent to ui.operator->(whatever). It's probably worth noting that the semantics of an overloaded operator-> are somewhat unusual: x-> is interpreted as (x.operator->())->y, so the overloaded operator-> must return some type for which returned_object->y is also legal -- either another object that also overloads operator->, or else an actual pointer.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111