tags:

views:

248

answers:

7

Why are we using * character when we are creating button but we aren't adding it to app instance?

#include <QApplication>
#include <QPushButton>

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    QPushButton *button = new QPushButton("Button Text");
    QObject::connect(button,SIGNAL(clicked()),&app,SLOT(quit()));
    button->show();
    return app.exec();
}

When should we use *, &, ., ->?

+5  A: 

new returns a pointer, hence the * is used here to define button as a pointer-to-QPushButton, The & is used to reference (get the address of) app, which is then passed to connect.

button->show(); 

is equivalent to:

(*button).show(); 

This is all basic C (except new) and C++ covered in any introductory book.

Matthew Flaschen
+1  A: 

* is used to indicate a pointer in C++ when creating objects. So QPushButton *button defines a pointer of type QPushButton. This pointer is used to store the memory address where the push button object resides. On the other hand, app is an object itself and not a pointer. Hence there is no * along with it. If you are accessing an object's properties through pointer, you use -> else you use . . You really need a good basic C book if you having confusions in syntax at this level.

Naveen
+7  A: 
  • You should use . when you want to access a member of a struct variable.
  • You should use * when you want to declare a pointer to something (e.g. a struct).
  • You should use & when you want to get a pointer to something from a variable.
  • You should use -> when you want to access a member of a struct from a pointer.
Didier Trosset
"You should use . when you want to access a public member of a class or struct variable" would be better IMHO
rubenvb
I wrote `struct` instead of `class or struct` for simplicity, you're right. On the other side, the member does not need to be public, it can be accessed from a friend function or class, or from within the class itself.
Didier Trosset
+1  A: 

Operator -> is introduced to easily get to the struct's(or class's in C++) members. Dereferencing a pointer is a lower precedence to evaluating a member (dot operator). Consider the following syntax:

some_struct * pStruct;
*pStruct.somemember = 5;

*pStruct.somemember = 5; is actually equivalent to *(pStruct.somemember) and it dereferences pStruct.somemember, not pStruct.

So in order to dereference pStruct first you have put it into parenthesis, which is a few extra characters: (*pStruct).somemember. pStruct->somemember is a shorthand, which looks a lot cleaner.

Igor Zevaka
+1  A: 

To refer to a member variable or method of an object, one must use the dot operator .

QApplication app(argc,argv);
return app.exec();

To refer to a member variable or method through a pointer to an object, one must use the right-arrow operator ->

QPushButton* button = new QPushButton("Button Text");
button->show();

To get the address of an object which is necessary when you have a function that expects a pointer to an object as in this case, one must use the address of operator &

QObject::connect(button,SIGNAL(clicked()),&app,SLOT(quit()));

To get the value of the object stored at some address, one must use the deference operator *

QPushButton* button = new QPushButton("Button Text");
//*button is the value stored at address button          
Khnle
A: 

QPushButton *button = ...

Note that this is an assignment to button, not to *button. Gotta love the C declarator syntax :) Maybe it would give you less headaches to write QPushButton* button = ..., but this won't work with multiple variables in a single declaration.

In a declaration, the * means that the variable being declared is a pointer. In an expression, the * means dereferencing.

FredOverflow
A: 

By default you should use nothing, only static allocation:

QPushButton button;
QApplication app(argc,argv);
std::string s;
int a = 0;

Class/struct members are accessed through ., as in : s.append( "test" )

When you pass it to a function, if you don't want a copy, use a reference

void myFunction( std::string & s ){}

myFunction( s );

If you don't want to modify the parameter but still don't want a copy, use a const reference:

void myFunction( const std::string & s ) {}

myFunction( s ); // we know s won't be modified

That's the basic, what you use by default. You see, it's quite simple. The variables are automatically freed when they go out of scope.

Then comes the pointers. You need to use pointers to allow dynamically the memory on the heap or free store.

int * p = new int[4]; // allow 4 doubles on the free store

p is the address of the first element of the 4 ints. *p is the value of the first int. In our example, p[1] gives the values of the second int. Class/struct members are accessed through -> (instead of .). -> is the equivalent of (*variable).

If you have a variable allocated statically as:

int i = 0;

then &i is the address and i is the value.

You may need pointers with variables that are not scoped-bound, with functions that take addresses as arguments, with polymorphism ( a child class pointer into a base class pointer )

Contrary to static allocation, you need to explicitly destroy pointers. Hence you use

delete[] p; // allocated with new[]
delete p2;  // allocated with new

This is where you can get memory leaks, if you forget to delete the memory.

This is why "smart pointers" where invented, these are pointers that will automatically delete their content when nobody needs to use them anymore. For instance, boost::shared_ptr or maybe your compiler support tr1::shared_ptr.

Used like this:

boost::shared_ptr< int > a( new int( 42 ) );

And no need to delete it

Nikko