[英]Accessing variables from one class to another in C++
I am trying to access a variable declared in class A
from class B
, without using static
variables. 我试图从类B
访问类A
声明的变量,而不使用static
变量。 I have the classes separated in header and source files. 我将类分为头文件和源文件。
I have seen different people using pass by reference (I assume "const &a" declared in the class definition) but it doesn't work for me. 我见过不同的人使用按引用传递(我假设在类定义中声明了“ const&a”),但它对我不起作用。
Update:When I tried to pass in the A object to the B::print as a const-reference parameter I got an error. 更新:当我尝试将A对象作为const引用参数传递给B :: print时,出现错误。 In my example, I am trying to access string a
from the function void print
declared in class B
. 在我的示例中,我尝试从class B
声明的void print
函数访问string a
。 The problem now is that I am getting an error in B.cpp
. 现在的问题是B.cpp
出现错误。
main.cpp main.cpp
#include <iostream>
#include <string>
#include "A.h"
#include "B.h"
using namespace std;
int main()
{
A first;
B second;
second.print(cout, first);
return 0;
}
Ah 啊
#include <string>
using namespace std;
class A
{
string a = "abc";
public:
A();
void print(ostream& o) const;
~A();
};
A.cpp 丙型肝炎
#include <iostream>
#include <string>
#include "A.h"
#include "B.h"
using namespace std;
A::A()
{
}
A::~A()
{
}
void A::print(ostream& o) const
{
o << a;
}
ostream& operator<<(ostream& o, A const& a)
{
a.print(o);
return o;
}
Bh h
#include <iostream>
#include <string>
#include "A.h"
using namespace std;
class B
{
public:
B();
void print(ostream&, A const&) const;
~B();
};
B.cpp 丙型肝炎
#include "B.h"
#include "A.h"
#include <iostream>
#include <string>
using namespace std;
B::B()
{
}
B::~B()
{
}
void B::print(ostream& o, A const& a) const
{
o << a << endl;
//^^ error no operator "<<" mathes these operands
}
The way I'd do it is to pass in the A object to the B::print as a const-reference parameter. 我要做的方法是将A对象作为const-reference参数传递给B :: print。 I'd also pass in the ostream as a reference parameter. 我还要将ostream作为参考参数传递。 And I'd take advantage of C++'s streaming output operator ( <<
). 而且我会利用C ++的流输出运算符( <<
)。
Like this: 像这样:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::ostream;
using std::string;
class A
{
std::string s = "abc";
public:
void print(ostream& o) const;
};
void A::print(ostream& o) const
{
o << s;
}
ostream& operator<<(ostream& o, A const& a)
{
a.print(o);
return o;
}
class B
{
public:
void print(ostream&, A const&) const;
};
void B::print(ostream& o, A const& a) const
{
o << a << endl;
}
int main()
{
A first;
B second;
second.print(cout, first);
}
UPDATE: given the comments above, I'm not not sure if the problem is "How does one split up code into separate .h and .cpp files?" 更新:鉴于以上评论,我不确定问题是否是“如何将代码拆分为单独的.h和.cpp文件?” or if it is "How do I access A member variables from B, without using static variables in A?" 或者是“如何从B访问A成员变量,而不在A中使用静态变量?”
UPDATE: I changed A's member variable from a
to s
to disambiguate from other a
identifiers. 更新:我改变了A的成员变量从a
到s
的歧义与其他a
标识符。
Since a
is not a static member, it can't be accessed without an instance of class A. You can, however, pass one in the function: 由于a
不是静态成员,因此没有类A的实例就无法访问它。但是,您可以在函数中传递一个:
class B {
void print(const A &o) {
cout << o.a << endl;
}
};
In addition, if a
member is private, you can declare class B
as friend, which means it can access private and protected members of class A
. 此外,如果a
成员是私有的,你可以声明class B
为好友,这意味着它可以访问private和protected成员class A
。
class A {
friend class B;
private:
std::string a = "abc";
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.