私は小さな C++ プログラムを構築しており、クラスにカスタム演算子を実装しています。また、私はSTLベクトルを扱っています。
しかし、私は最初に立ち往生しています。これが私のインターフェースクラスです:
class test {
vector<string> v;
public:
vector<string>& operator~();
};
そして、実装は次のとおりです。
vector< string>& test::operator~(){
return v;
}
ベクトルへの参照を返したいので、メインプログラムでこのようなことができます
int main(){
test A;
vector<string> c;
c.push_back("test");
~A=c;
//i want to do it so the vector inside the class takes the value test,thats why i need reference
}
アップデート
プログラムは動作しますが、そのクラス属性への参照を返しません。次に例を示します。
私がこのようなものを持っている場合:
int main(){
test A;
A.v.push_back("bla");
vector<string> c;
c=~A;
//This works, but if i want to change value of vector A to another vector declared in main
vector<string> d;
d.push_back("blabla");
~A=d;
//the value of the A.v is not changed! Thats why i need a reference to A.v
}