gpt4 book ai didi

c++ - 在 C++ 中访问不同类中的类的成员

转载 作者:太空狗 更新时间:2023-10-29 20:55:30 24 4
gpt4 key购买 nike

我正在尝试使用 C++ 理解面向对象的编程。以下是一个最小的示例,其结果不是我天真的期望:

#include <iostream>

class B {
public:
B (int val) : val(val) {;}

int get_val() { return val; }
int set_val(int a) { val = a; }

private:
int val;
};

class A {
public:
A (B b) : b(b) {;}
B get_b() { return b; }

private:
B b;
};

int main(){
B b_main(5);
std::cout << b_main.get_val() << std::endl; // Prints 5, which makes sense

A a_main(b_main);
std::cout << a_main.get_b().get_val() << std::endl; // Prints 5, which makes sense

a_main.get_b().set_val(2);
std::cout << a_main.get_b().get_val() << std::endl; // Why does this not print 2?

return 0;
}

最后的 cout 语句对我来说没有意义。在倒数第二行中,我将对象的值设置为 2,那么为什么不打印 2?查看 Stack Exchange 上的一些类似问题,我发现了一些让 A 和 B 成为彼此 friend 的建议。我尝试在 B 类中添加 friend class A 并在 A 类中添加 friend class B ,但这没有用。在我的理解中,添加 friend 语句应该是不必要的,因为我在类 A 中有 get_b() 方法。我发现了一些建议尝试通过引用 A 的构造函数来传递类型 B 的对象: A (B& b) : b(b) {;} 但这也不起作用。

任何人都可以向我解释为什么程序没有产生预期的结果以及如何获得预期的结果(即最后的 cout 语句打印 2)?

注意:我还尝试了以下内容。我将类 A 的私有(private)变量 b 公开:

#include <iostream>

class B {
public:
B (int val) : val(val) {;}
int get_val() { return val; }
int set_val(int a) { val = a; }

private:
int val;
};

class A {
public:
A (B b) : b(b) {;}
B b; // This is now public
//B get_b() { return b; } // This is no longer needed

private:
};


int main(){
B bmain(5);
std::cout << bmain.get_val() << std::endl;

A amain(bmain);
std::cout << amain.b.get_val() << std::endl;

amain.b.set_val(2);
std::cout << amain.b.get_val() << std::endl; // Works!

return 0;
}

现在我得到了想要的结果。与第一个代码片段相比,这是代码的实现方式吗?我想要第一个代码片段中的 get_b() 方法,但如果这不是解决此问题的正确方法,请告诉我。

最佳答案

In the second to last line, I set the value of the object to be 2, so why does this not print 2?

因为您使用 get_b() 方法返回 a_mainB 对象的拷贝a_main 中的 b 变量被复制,即 B 类的另一个对象,与 b 相同> 成员,被创建,并返回给调用者。然后,修改新的 B 对象。但它与a_main中的原始b没有联系。这与可见性和成员访问权限无关。

但是,在第二个示例中,您在a_main 中暴露了b 成员,并直接对该对象进行操作,而没有复制它,因此结果是成功的。 public 修饰符改变的是它允许您直接访问 b 对象,因此效果。

I found some suggestions to try passing the object of type B in by reference to the constructor of A: A (B& b) : b(b) {;} but this did not work either.

那是行不通的。当您这样做时会发生什么,A::b 使用通过引用传递的值 true 进行初始化。但是引用 只会导致没有将 b 的额外拷贝传递给正在制作的构造函数。此引用不会在传递给构造函数的 bA::b 之间创建链接。它在另一端,可以这么说。

顺便说一句,A (B& b) : b(b) {;} c'tor 参数名称与成员名称相同是一个不好的做法。将它们命名为相似的是个好主意,但仍然添加例如下划线:A (B& _b) : b(_b) {;}

如果你想在第一个片段中获得相同的结果,返回一个 referenceb 像这样:

  B& get_b() { return b; }

不过,这是不可取的,因为您暴露类 A 的私有(private)成员只是为了允许 A 的客户端修改该成员的特定属性。最好在 A 中提供一种方法来设置 A::bval 属性,而无需完全访问 A::b.

肯定看到这个:What's the difference between passing by reference vs. passing by value?

也许这个:Java and C++ pass by value and pass by reference

因为我觉得您来自 Java,并期望默认情况下在 C++ 中按引用传递。

关于c++ - 在 C++ 中访问不同类中的类的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35540495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com