- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 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_main
中 B
对象的拷贝。 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
的额外拷贝传递给正在制作的构造函数。此引用不会在传递给构造函数的 b
和 A::b
之间创建链接。它在另一端,可以这么说。
顺便说一句,A (B& b) : b(b) {;}
c'tor 参数名称与成员名称相同是一个不好的做法。将它们命名为相似的是个好主意,但仍然添加例如下划线:A (B& _b) : b(_b) {;}
如果你想在第一个片段中获得相同的结果,返回一个 reference 给 b
像这样:
B& get_b() { return b; }
不过,这是不可取的,因为您暴露类 A
的私有(private)成员只是为了允许 A
的客户端修改该成员的特定属性。最好在 A
中提供一种方法来设置 A::b
的 val
属性,而无需完全访问 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/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!