Страница 1 из 1

оператор this

Добавлено: 2008-01-28 1:00:22
100kg

Код: Выделить всё

zProc = new QProcess(this);
zProc->execute(cWgetLine);
а что будет если мы неявно обратимся тоесть zProc.execute(cWgetLine)? заметил что явное обращение происходит когда динамически создаем объекты.

Re: оператор this

Добавлено: 2008-01-28 11:50:21
Fastman
100kg писал(а):

Код: Выделить всё

zProc = new QProcess(this);
zProc->execute(cWgetLine);
а что будет если мы неявно обратимся тоесть zProc.execute(cWgetLine)? заметил что явное обращение происходит когда динамически создаем объекты.
Можно конечно, но:
Стек не резиновый. Много нельзя запихнуть. А при работе с потоками вообще есть ограничение которое зависит от операционной системы :

void QThread::setStackSize ( uint stackSize )
Sets the maximum stack size for the thread to stackSize. If stackSize is greater than zero, the maximum stack size is set to stackSize bytes, otherwise the maximum stack size is automatically determined by the operating system.
Warning: Most operating systems place minimum and maximum limits on thread stack sizes. The thread will fail to start if the stack size is outside these limits.

С++ не для удобства - а для написания программ :))) И понимания управления памаятью - один из самых непростых вопросов. Плюс многопоточные приложения не так просты как кажутся )

Re: оператор this

Добавлено: 2008-01-28 15:08:49
100kg
ясно, я просто хотел узнать что будет если обратиться zProc.execute(cWgetLine), оказываеться компилятор ругаеться,
вот что написал для проверки

Код: Выделить всё

#include <iostream>
#include <new>
 
using std::cout;
using std::cin;
using std::endl;
 
class Test {
 
 public:
  Test (int = 0, int = 0);  
  ~Test();
 
  void  setX(int);
  void  setY(int);
 
  int getX();
  int getY();
 
  void printall();  
  
 private:
  int x;
  int y;
};
 
Test::Test(int  Xvalue, int  Yvalue )
{
 
 setX(Xvalue);
 setY(Yvalue);
 
}
 
void Test::setX(int Xvalue) {
 
 x=Xvalue;
 
}
 
void Test::setY(int Yvalue) {
 
 y=Yvalue;
 
}
 
int Test::getX() {
 
return x;
 
}
 
int Test::getY() {
 
return y;
 
}
 
void Test::printall() {
 
  cout << "X value is: " << getX() << endl
       << "Y value is: " << getY() << endl;
}
 
int main () {
 
 Test *t1ptr;
 t1ptr = new Test(3,4);
 
/*must be t1ptr->printall(); or (*t1ptr).printall();
 t1ptr.printall(); gives error */
 
 t1ptr->printall(); 
 
 return 0;
}
если t1ptr.printall();

Код: Выделить всё

test.cpp:70: error: request for member `printall' in `t1ptr', which is of non-class type `Test*'

Re: оператор this

Добавлено: 2008-01-28 15:45:43
Fastman
Естесственно. ты же сделал

Код: Выделить всё

Test *t1ptr;
t1ptr = new Test(3,4);
Так будь добр обращаться по указателю.. то есть через "->"
А если бы ты написал:

Код: Выделить всё

Test t1ptr(3,4);
t1ptr.printall();
Оно бы скомпилилось.
По пробую пояснить:
В твоем случае (Test *t1ptr;) t1ptr - является указателем на объект класса.
В языке С++ "->" это операция вызова УКАЗАТЕЛЯ класса а "." члена класса.
Не забывай что если ты определяешь Test t1ptr(3,4); то область жизни t1ptr будет в пределах члена класса.
то есть:

Код: Выделить всё

void SomeOtherClass::foo()
 {
Test t1ptr(3,4);
t1ptr.printall();
//при выходе t1ptr будет уничтожен
}
а в случае ессли сделаешь так например

Код: Выделить всё

class SomeOtherClass
{
    SomeOtherClass();
    ~SomeOtherClass();
    void foo();
    void foo2();
    Test *t1ptr;
}

SomeOtherClass::SomeOtherClass()
{
    t1ptr = new Test(3,4);
}

void SomeOtherClass::foo()
{
    //можем тут напечатать твои значения
     t1ptr->printall();
}

void SomeOtherClass::foo2()
{
    //и тут можем напечатать твои значения
     t1ptr->printall();
}

//И !!! Не забываем сделать !!!!
SomeOtherClass::~SomeOtherClass()
{
    if(t1ptr != NULL)
       delete t1ptr;
}
как то вот так.

Re: оператор this

Добавлено: 2008-01-28 16:42:50
100kg
ах да забыл удалить указатель. все спасибо.