PDA

View Full Version : Blonde Moment



Atomic_Sheep
28th January 2012, 05:57
I'm having a blonde moment and I can't figure out something that I know is simple.

I have a standard private slot:

void function : : on_PushButton_clicked() //ps I used spaces here because otherwise it shows up as smileys on the forum.
{
QString address = ui->LineEdit->text();
}

and I have created a class called fileManager which has a function fileOpen.

How do I get this to work?

fileManager data;
data.fileOpen(address);

At the moment, I've got a permission error due to the clicked slot being private.

Can I use pointers? I'm not sure how to use them 100% quite yet so after trying this:

fileManager data
data.fileOpen(*address)

and then

void fileManager::fileOpen(QString &fileDirectory)
{
QDir directory(fileDirectory);
QMessageBox currentlFileDirectory;
currentlFileDirectory.setInformativeText(fileDirec tory);
int ret = currentlFileDirectory.exec();
}

This doesn't seem to work, so maybe I'm not declaring something correctly in respective header files?

wysota
28th January 2012, 11:00
Your slot declares a local variable that goes out of scope immediately so you can't use it in other methods. Declare the address variable as a member of the class and skip the asterisk when accessing it.

Atomic_Sheep
28th January 2012, 12:10
I don't understand why, but what you suggested, worked... no surprises there, thanks very much. Now I just gotta figure out why it works.

wysota
28th January 2012, 12:29
Consider this:


class A {
public:
void method1() { QString x = "one"; }
void method2() { QString x = "two"; }
QString getter() const { return x; }
};

What do you think, what would the value returned by calling getter() be and why?

Atomic_Sheep
29th January 2012, 00:01
Well, if you do this:

const A AClass;
AClass.getter();

Then you should get back whatever x was assigned to in the definitions part of the class which is impossible to determine from the above information because you didn't provide the assgined value to x?

wysota
29th January 2012, 00:43
What definition part of the class? "x" is not defined anywhere in the class. Now look at your original code, you have exactly the same situation. Did your code even compile?

Atomic_Sheep
1st February 2012, 16:01
Well, I still don't quite understand your question as I'm still not fully grasping the idea of const quite yet, however I did understand why what you suggested worked...

Basically, when I call a function from another class from within the first class:

fileManager data;
data.fileOpen(address);

when the fileOpen(QString fileAddress function starts), all that happens is a new object is created called fileAddress and the QString address = ui->LineEdit->text() value is placed into that variable.

So really basic, I just got confused when seeing more complex paths and variables.

However getting back to your example, even though as I said... I'm not too sure, I think the value you will get when you call getter() will be 'x' as getter is a QString and I think all the const does is that the caller will not be able to alter the string name from 'x'.

KillGabio
1st February 2012, 16:52
What happens is that you define a new 'x' every time you call any of those two methods. That x is only available in that method and it is destroyed when it finishes.

wysota
1st February 2012, 19:40
However getting back to your example, even though as I said... I'm not too sure, I think the value you will get when you call getter() will be 'x' as getter is a QString and I think all the const does is that the caller will not be able to alter the string name from 'x'.

The point is this example will not even compile, it's just semantically incorrect. In C++ the fact that you give things the same name doesn't mean they are related in any way. I can create hundreds of variables called x that will have different values. See the code below:


int x;

class A {
public:
A() { int x; x = 8; this->x = 7; }
void setX(int x) { this->x = x; }
void getX() const { return x; }
void method() {
int x;
{
double x = 7.8;
{
int x = 5;
}
}
}
private:
int x;
};

class B {
public:
B() { x = 7; }
void method() { int x = 8; }
private:
int x;
};

int func() { int x = 2; return x; }

I suggest you analyze it until you fully understand it.