Reputation: 3881
I have a pointer to a class and I'm trying to use it to access the class' public struct. I've looked at access member var using ptr, as well as access memb struct from ptr class, but when you look at the links, it's not what I'm trying to do.
I'm having trouble doing something that will build. The examples in the code are without pointers, but I have a pointer to IFM to work with. Does anyone know how to use the pointer (to IFM) to access the public struct (in IFM)?
//snippet of code that is trying to access struct in IFM:
const IFM *pJunk = rData1.getM(); //this is fine
pJunk->JunkStruct::Junk.xs; //this doesn't work
The struct in IFM.h:
class IFM final : public IFO
{
public:
typedef struct JunkStruct
{
JunkStruct() = default;
~JunkStruct() = default;
JunkStruct(const IFM::JunkStruct&) = default;
JunkStruct(const double& first, const double& second, const double& third, const double& fourth, const double& fifth, const double& sixth) :
xs(first), ysk(second), xsk(third), ys(fourth), x(fifth), y(sixth)
{}
IFM::JunkStruct& operator=(const IFM::JunkStruct&) = default;
// Initialized
double xs = 1.0;
double ys = 0.0;
double xsk = 0.0;
double ysk = 1.0;
double x = 0.0;
double y = 0.0;
} Junk;
...
Upvotes: 0
Views: 84
Reputation: 14705
OPs member is private. It is designed to be hidden. Nothing to see here.
The struct is just a type. If no members in IFM is of type JunkStruct there is no data there to be accessed. To use that inner type you can see this minimal example
struct Outer
{
struct Inner{
int innerMember;
};
};
int main() {
Outer::Inner inner;
inner.innerMember = 4;
std::cout << inner.innerMember << "\n";
Outer outer;
Outer* outerP = &outer;
outer-> No access to innerMember, it does not exist.
}
If on the other hand, the struct would not be a typedef but just a struct, that definition would be akin to:
struct Outer
{
struct Inner{
int innerMember;
};
Inner outerMemberInner; //
};
int main() {
Outer outer;
Outer* outerP = &outer;
outerP->outerMemberInner.innerMember = 4;
// this might be what you are looking for
std::cout << outerP->outerMemberInner.innerMember;
}
With Junk
as outerMemberInner
.
The type JunkStruct aliased Junk belongs to the type IFM, not to IFM objects or pointer.
Upvotes: 1