从 in-class 结构方法中获取对 class 的引用
Get reference on class from in-class struct method
我有这个代码:
//.h
class A
{
struct B
{
void SomeMethod();
}
B b;
}
//.cpp
void A::B::SomeMethod()
{
//here will be code
}
我可以从 SomeMethod()
得到 link 到 class A
的对象吗?因为 this
return 引用结构对象 b
.
也许答案很容易找到,但我无法提出正确的要求。
感谢您的帮助。
从设计(B 是 A 的私有成员)看来用户只能访问 A class 接口。那么将父 class 指针传递给方法怎么样:
class A
{
struct B
{
void SomeMethod(A* parent);
};
B b;
public:
void callSomeMethod() { b.SomeMethod(this); }
};
//.cpp
void A::B::SomeMethod(A* parent)
{
std::cout << "parent addr: " << parent << std::endl;
}
int main()
{
A a;
std::cout << "a addr: " << &a << std::endl;
a.callSomeMethod();
}
我有这个代码:
//.h
class A
{
struct B
{
void SomeMethod();
}
B b;
}
//.cpp
void A::B::SomeMethod()
{
//here will be code
}
我可以从 SomeMethod()
得到 link 到 class A
的对象吗?因为 this
return 引用结构对象 b
.
也许答案很容易找到,但我无法提出正确的要求。
感谢您的帮助。
从设计(B 是 A 的私有成员)看来用户只能访问 A class 接口。那么将父 class 指针传递给方法怎么样:
class A
{
struct B
{
void SomeMethod(A* parent);
};
B b;
public:
void callSomeMethod() { b.SomeMethod(this); }
};
//.cpp
void A::B::SomeMethod(A* parent)
{
std::cout << "parent addr: " << parent << std::endl;
}
int main()
{
A a;
std::cout << "a addr: " << &a << std::endl;
a.callSomeMethod();
}