
Originally Posted by
Atomic_Sheep
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; }
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; }
To copy to clipboard, switch view to plain text mode
I suggest you analyze it until you fully understand it.
Bookmarks