2013-07-29 58 views
2

當我嘗試使用一種方法返回一個私有變量時,它似乎從構造對象以來的值發生了變化。這是我的代碼和輸出。C++:返回一個類變量

的main.cpp

#include <iostream> 
#include "coordinate.h" 

using namespace std; 

int main() 
{ 
    Coordinate c(1, 1); 
    cout << c.getX() << endl; 

} 

coordinate.cpp

#include "coordinate.h" 
#include <iostream> 

using namespace std; 

Coordinate::Coordinate(int x, int y) 
{ 
    x = x; 
    y = y; 

    cout << x << endl; 

} 

coordinate.h

#ifndef COORDINATE_H 
#define COORDINATE_H 

class Coordinate 
{ 
    private: 
     int x; 
     int y; 

    public: 
     Coordinate(int x, int y); 

     int getX() { return x; } 
     int getY() { return y; } 
}; 

#endif 

Output

+2

請勿張貼文字的屏幕截圖。 –

回答

8

你的構造函數分配給它的參數,而不是日e對象的私有字段。使用初始化列表,或者明確地this限定分配對象,或選擇不同的參數名:

Coordinate::Coordinate(int x, int y) : x(x), y(y) { 
    cout << x << endl; 
} 

Coordinate::Coordinate(int x, int y) { 
    this->x = x; 
    this->y = y; 
    cout << x << endl; 
} 

Coordinate::Coordinate(int xVal, int yVal) { 
    x = xVal; 
    y = yVal; 
    cout << x << endl; 
} 
+3

甚至更​​好,使用構造函數的初始化列表,並且不需要'this->'。 'Coordinate :: Coordinate(int x,int y):x(x),y​​(y){...}' – Praetorian

+0

@Praetorian:好點。我更習慣於Java,它沒有這些。 – user2357112

+0

我現在修好了,謝謝。 –

0

嘗試:

Coordinate::Coordinate(int x, int y) 
{ 
    this->x = x; 
    this->y = y; 

    cout << this->x << endl; 

} 

第一次獲得x = 1的值,因爲它的參數值'x'已打印。但第二次你錯了,因爲成員變量x從來沒有得到任何值分配。

1

在構造函數中,x引用的是參數,而不是成員變量,所以x = x是將參數賦值給它自己。成員變量保持未初始化狀態。

您可以通過使用成員初始化程序列表或通過this->x明確引用成員變量來避免此問題。

Coordinate::Coordinate(int x, int y) : x(x), y(y) 
{ 
    cout << this->x << endl; 
} 
0

您是否嘗試使用this這樣的指針將值分配給私有成員變量?

Coordinate::Coordinate(int x, int y) 
{ 
    this->x = x; 
    this->y = y; 

    cout << x << endl; 
} 

或者你可以做的是改變在構造函數中的參數名稱,以避免使用this指針

Coordinate::Coordinate(int a, int b) 
{ 
    x = a; 
    y = b; 

    cout << x << endl; 
} 
0

問題是與這些任務:

x = x; 
y = y; 

你實際上是分配構造函數參數xy自己,而不是從參數到對象的xy成員。

另外,該線路

cout << x << endl; 

打印構造函數的參數x而不是對象的x成員。

您通過使用與構造函數參數名稱相同的名稱來隱藏成員xy引用名稱xy而不限定它們將引用參數而不是對象的成員。

您可以通過執行類似this的方法來解決此問題。在那裏,我以m_作爲前綴的成員變量。你也可以做一些其他類似的技巧。

0

uselease更換變量名稱:

class Coordinate 
{ 
    private: 
     int a; 
     int b; 

    public: 
     Coordinate(int x, int y) 
     { 
     a = x; 
     b = y; 

     cout << x << endl; 
     } 

     int getX() { return a; } 
     int getY() { return b; } 
}; 

其實編譯器感到困惑x的值使用,PLZ使用私人部分其他一些變量,。否則,你可以用它來也解決此爲:

Coordinate(int x, int y) 
     { 
     this->x = x; 
     this->y = y; 

     cout << x << endl; 
     } 
0

有了這些代碼:

x = x; 
y = y 

你是爲x和y自己。

Coordinate::Coordinate(int xVal, int yVal) 
{ 
    x = xVal; 
    y = yVal; 

    cout << x << endl; 
} 

沒問題。

最好的辦法是:

Coordinate::Coordinate(int xVal, int yVal):x(xVal),y(yVal) 
{ 
    cout << x << endl; 
} 
0

正如其他人所指出的

Coordinate::Coordinate(int x, int y) 
{ 
    x = x; 
    y = y; 

這是一個 「影子變量」 的條件。函數原型中的值「x」和「y」映射成員變量,因此「x = x」將參數x的值分配給參數x。

像GCC和Clang這樣的編譯器會向你發出警告。 MSVC並不是因爲微軟的API有點亂,而且他們使用了很多符號表的地獄:)

避免這種情況的一種廣泛使用的方法是前綴。 「m_」代表「成員」,「g_」代表「全球」,「s_」代表「靜態」。

class Coordinate 
{ 
    // by saying 'class' instead of 'struct', 
    // you declared the initial state to be "private". 
    int m_x; 
    int m_y; 
    ... 


Coordinate::Coordinate(int x, int y) 
    : m_x(x) 
    , m_y(y) 
{} 

有些人會更進一步,爲參數名稱使用前綴或後綴;我選擇了從我工作的幾個開源項目中添加「_」。

Coordinate::Coordinate(int x_, int y_) 
    : m_x(x_) 
    , m_y(y_) 
{ 
    int x = m_x; // assign from member value 
    int y = y_; // assign from parameter 
    std::cout << x << ", " << y << std::endl; 
}