黑马程序员技术交流社区
标题:
一个困扰我很久的c++问题
[打印本页]
作者:
jiang123456
时间:
2021-9-12 10:29
标题:
一个困扰我很久的c++问题
#include <iostream>
#include <vector>
class C {
public:
C() = default;
~C() = default;
double x() { return x_; }
void set_x(double x) { x_ = x; }
double y() { return y_; }
void set_y(double y) { y_ = y; }
protected:
double x_;
double y_;
};
class B {
public:
B() = default;
~B() = default;
void set_c(std::vector<C> c) { c_ = c; }
std::vector<C> c() { return c_; }
std::vector<C> *mutable_c() { return &c_; }
protected:
std::vector<C> c_;
};
class A {
public:
A() = default;
~A() = default;
B b() { return b_; }
void set_b(B b) { b_ = b; }
protected:
B b_;
};
int main() {
A a;
B b;
C c1, c2;
std::vector<C> c;
c1.set_x(11.11);
c1.set_y(22.22);
c.push_back(c1);
c2.set_x(33.33);
c2.set_y(44.44);
c.push_back(c2);
b.set_c(c);
a.set_b(b);
for (auto it : *(a.b().mutable_c())) {
std::cout << "111:" << it.x() << std::endl;
std::cout << "222:" << it.y() << std::endl;
}
std::cout<<std::endl;
for (auto it : a.b().c()) {
std::cout << "333:" << it.x() << std::endl;
std::cout << "444:" << it.y() << std::endl;
}
return 1;
}
执行结果:
111:0
222:4.64953e-310
111:33.33
222:44.44
333:11.11
444:22.22
333:33.33
444:44.44
为什么???
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2