我是C ++的初学者,我刚开始学习OOP。在下面的程序中,我添加了相同类的对象并显示结果。但是,我无法理解这样一个事实:如果我通过值将对象传递给函数,那么更改是如何在调用函数中反映出来的。 addNumbers()
函数需要Complex类的两个对象和用于调用函数的对象(c3.addNumbers(c1, c2)
)被隐式传递给函数,但c3.real
和{的值如何? {1}}在调用函数中受影响,因为c3.imaginary
无法访问内存中的“位置”。任何帮助将不胜感激!
提前致谢!
addNumbers()
答案 0 :(得分:3)
当您致电c3.addNumbers(c1, c2))
时,addNumbers
会将指针隐含地收到c3
而不是c3
的副本。此指针可以与this
关键字一起明确使用。
所以你的功能可以像这样重写:
void complex::addNumbers(complex c1, complex c2)
{
this->real = c1.real + c2.real;
this->imaginary = c1.imaginary + c2.imaginary;
}
严格等同于原始的addNumbers
函数。
换句话说:每次在成员函数中使用类成员时,隐含的this->
都会被添加到该成员中;因此,如果member
是类成员,那么member
总是等同于类成员函数中的this->member
。
答案 1 :(得分:1)
我在您的原始代码中做了一些评论,以解释为什么真实和想象在下面会产生影响。 (寻找// MABVT)
另外: 我将提供另一个有用的例子让你进一步发展!
查看强>
class complex {
private:
int real;
int imaginary;
public:
/* Using member initializers to assign values to members */
complex()
: real(0)
, imaginary(0)
{}
void readData(int x, int y);
void printData();
// MABVT: You provide two complex numbers which you want to add
// together!
void addNumbers(complex, complex);
};
void complex::readData(int x, int y)
{
real = x;
imaginary = y;
}
void complex::printData()
{
cout << real << "+" << imaginary << "i" << endl;
}
void complex::addNumbers(complex c1, complex c2)
{
// MABVT: Use c1.component and c2.component, add them up and store them
// in this class' instance.
real = c1.real + c2.real;
imaginary = c1.imaginary + c2.imaginary;
// MABVT: c3.real and c3.imaginary are affected at this exact location
// since you overwrite the values with the addition-results.
// Since the function addNumbers(complex, complex) is invoked
// on the complex instance 'c3', real and imaginary of c3 are
// known in this context, and consequently you can use them.
//
// To attach to your statement that the c3 instance's pointer is
// implicitly passed:
// Yes it is passed as the first parameter invisibly as
// 'complex* this'
//
// So you could also write:
// this->real = c1.real + c2.real; (see the use of this?)
}
int main(void)
{
complex c1, c2, c3;
c1.readData(-5,17);
c2.readData(11,7);
c3.addNumbers(c1,c2);
c3.printData();
return 0;
}
<强> ALTERNATIVE 强>
// Example program
#include <iostream>
#include <string>
class Complex { // Give class names capital first letter
private:
int m_real; // Just a recommendation: I'd like to be able to distinguish parameter for member in the identifier already!
int m_imaginary; // Just a recommendation: I'd like to be able to distinguish parameter for member in the identifier already!
public:
/* Using member initializers to assign values to members */
inline Complex() // Inline it, if you define this class in a header and reuse it multiple times...
: m_real(0)
, m_imaginary(0)
{}
// Provide initializing constructor to be able to construct
// a complex number quickly. Replaces your readData(...);
inline Complex(
int inRealPart,
int inImaginaryPart)
: m_real(inRealPart)
, m_imaginary(inImaginaryPart)
{}
// Getters to read the values
inline int real() const { return m_real; }
inline int imaginary() const { return m_imaginary; }
void printData();
// Local assignment-add operator to add another complex
// to this specific instance of complex and modify the internal
// values. Basically what you did as the second part of addNumbers.
Complex& operator+=(const Complex& r);
};
void Complex::printData()
{
std::cout << m_real << "+" << m_imaginary << "i" << std::endl;
}
// Member add-assign operator definition adding this instance and another instance 'r' by adding up the values and storing them in the instance this operator is called on.
Complex& Complex::operator +=(const Complex& r)
{
std::cout << "Local" << std::endl;
this->m_real += r.real();
this->m_imaginary += r.imaginary();
return *this;
}
// Static global operator+ definition, taking two values and creating a
// third, NEW one initialized with the results.
// This was the first part of addNumbers
static Complex operator+(const Complex& l, const Complex& r) {
std::cout << "Static Global" << std::endl;
return Complex(
(l.real() + r.real()),
(l.imaginary() + r.imaginary())
);
}
int main(void)
{
// Same as before
Complex c1(-5, 17);
Complex c2(11, 7);
Complex c3(1, 2);
// Test output
c1.printData();
c2.printData();
c3.printData();
std::cout << std::endl;
Complex c3 = (c1 + c2); // Calls static global and c3 is overwritten with the result. Exactly like your addNumbers call
c1 += c2; // instance local, will change c1's internal values ( see print out below )
Complex c5 = ::operator+(c1, c2); // Static global, c5 is initialized with the result. Exactly like your addNumbers call
std::cout << std::endl;
c1.printData();
c2.printData();
c3.printData();
c5.printData();
return 0;
}
作为初学者,这应该是非常适合你的。
一些解释
静态全局与本地运营商重载
阅读主题:http://en.cppreference.com/w/cpp/language/operators
您使用的所有运算符(+, - ,*,/,%,+ =, - =,...)都只是为基本类型预定义的函数,由libstd为STD类型提供。
你可以覆盖/定义它们。
我是以两种方式做到的:
静态全局运算符+:
接受两个任意复杂实例并添加其组件。 最后创建一个新实例并使用结果进行初始化。
基本上这只是一个静态函数,它与“+”链接 编译器。
和
本地会员运营商+ =:
接受Complex的另一个实例并将其组件值添加到 调用运算符的实例的组件值:`l + = r - &gt;在l上调用,其值将通过添加r'
的值来修改必须定义所有op-assignment运算符(+ =, - =,* =,/ =等...) 在课堂上,既不是全球性的,也不是静态的。
const类型&amp;
阅读有关const:https://www.cprogramming.com/tutorial/const_correctness.html
的更多信息对任何类型的实例的Const引用将为您确保两件事:
组合意味着:您不必复制实例(按值传递),而只提供它的地址引用(按引用传递)。通常可以提高性能,尤其是在传递大型复杂对象时。
答案 2 :(得分:0)
虚构和真实是私有属性,但可以通过成员函数(也称为对象的方法)访问它们。执行c3.addNumbers(c1,c2)语句时,它将等同于以下两个语句:
c3.real = c1.real + c2.real;
c3.imaginary = c1.imaginary + c2.imaginary
我们可以访问c3.real和c3.imaginary的原因是因为addNymbers()函数是Complex类的成员。