N
Nick Valeontis
Hmmm, i have been playing around with cpp, but, honestly, I need some help
on this one...
Could someone explain to me why this does work as it does?
************** ************** ************** **************
#include <iostream>
using namespace std;
class Value {
public:
int v;
Value(int value) {
cout << "const!";
v = value;
}
};
class T {
public:
Value& value;
T(Value value):value(value) {
}
};
int main(int argc, char** argv) {
Value v(5);
T t(v);
cout << v.v;
cout << t.value.v;
//t.value.v++; // output is 5555 !!?
v.v++; // output is 5566
cout << v.v;
cout << t.value.v;
}
************** ************** ************** **************
while: ( the difference is on the constructor of T)
************** ************** ************** **************
#include <iostream>
using namespace std;
class Value {
public:
int v;
Value(int value) {
cout << "const!";
v = value;
}
};
class T {
public:
Value& value;
T(Value& value):value(value) {
}
};
int main(int argc, char** argv) {
Value v(5);
T t(v);
cout << v.v;
cout << t.value.v;
//t.value.v++; // output is 5566
v.v++; // output is 5566
cout << v.v;
cout << t.value.v;
}
************** ************** ************** **************
any idea?:/
thanks
-Nick
on this one...
Could someone explain to me why this does work as it does?
************** ************** ************** **************
#include <iostream>
using namespace std;
class Value {
public:
int v;
Value(int value) {
cout << "const!";
v = value;
}
};
class T {
public:
Value& value;
T(Value value):value(value) {
}
};
int main(int argc, char** argv) {
Value v(5);
T t(v);
cout << v.v;
cout << t.value.v;
//t.value.v++; // output is 5555 !!?
v.v++; // output is 5566
cout << v.v;
cout << t.value.v;
}
************** ************** ************** **************
while: ( the difference is on the constructor of T)
************** ************** ************** **************
#include <iostream>
using namespace std;
class Value {
public:
int v;
Value(int value) {
cout << "const!";
v = value;
}
};
class T {
public:
Value& value;
T(Value& value):value(value) {
}
};
int main(int argc, char** argv) {
Value v(5);
T t(v);
cout << v.v;
cout << t.value.v;
//t.value.v++; // output is 5566
v.v++; // output is 5566
cout << v.v;
cout << t.value.v;
}
************** ************** ************** **************
any idea?:/
thanks
-Nick