Pointers to nested structs

J

Joolz

Could someone please give me an example of how to make pointers to
nested structures? Pointers to normal structures are no problem...

struct structA {
int a;
}structA_s;

structA *pStructA = &structA_s;

But when I've got a nested structure, I can't make a pointer to that
structure.

struct structA {
int a;

struct structB {
int b;
}structB_s;

}structA_s;
structA *pStructA = &structA_s;

Now how do I make a pointer to an object of structB? I -can- reference
members of an object of a nested struct with i.e
pStructA->structB_s.b but that's without pointers.

Thanks in advance.
Jules
 
Z

Zara

Could someone please give me an example of how to make pointers to
nested structures? Pointers to normal structures are no problem...

struct structA {
int a;
}structA_s;

structA *pStructA = &structA_s;

But when I've got a nested structure, I can't make a pointer to that
structure.

struct structA {
int a;

struct structB {
int b;
}structB_s;

}structA_s;
structA *pStructA = &structA_s;

Now how do I make a pointer to an object of structB? I -can- reference
members of an object of a nested struct with i.e
pStructA->structB_s.b but that's without pointers.

Thanks in advance.
Jules

structA::structB *pStructB=&structA_s.structB_s;

that's all
 
J

Joolz

Cheers, now I can access the members by using pStructB->b , but now
(for me) the nesting-aspect is gone.

I want to access b like pStructA->pStructB->b

Thanks though :)
 
M

mlimber

Joolz said:
Cheers, now I can access the members by using pStructB->b , but now
(for me) the nesting-aspect is gone.

I want to access b like pStructA->pStructB->b

Thanks though :)

You can simply access your structB elements with the . operator:

pStructA->structB_s.b

You could make a pointer as a member of structA if your really want to,
but then you'll need to initialize it in the constructor:

struct A
{
int aDatum;
struct B
{
int bDatum;
};
B* const pB;

A() : pb( new B ) {}
~A() { delete pb; }
};

A *pA = new A;
pA->pB->bDatum = 42;
// ...
delete pA;

Or better you could use std::auto_ptr or boost::scoped_ptr for pB.

Cheers! --M
 
J

Joolz

Thanks, that works - allthough it looks a tad difficult...

Just to clear some things up, it isnt possible to access the variables
like you've shown (pA->pB->variable) but without the new/delete and
constructor/destructor things? That would help me out alot.

Cheers
J
 
M

mlimber

Joolz said:
Thanks, that works - allthough it looks a tad difficult...

Just to clear some things up, it isnt possible to access the variables
like you've shown (pA->pB->variable) but without the new/delete and
constructor/destructor things? That would help me out alot.

Cheers
J

You can, but the member pB needs to be a pointer. You should generally
avoid new and delete if they aren't necessary, which translates to
allocating objects automatically on the stack.

Perhaps you just think that pA->pB->variable looks prettier; you could
do the same sort of thing with the . operator and references (which are
like const pointers) in many cases:

struct A
{
int aDatum;
struct B
{
int bDatum;
};
B b;
};

void Foo( A& a )
{
a.aDatum = 0xc0ffee;
a.b.bDatum = 42;
}

void Bar()
{
A a;
Foo( a );
// ...
}

Cheers! --M
 
J

Joolz

lol - 0xc0ffee

Well you're right though, the -> operator is easier to 'spot' for me
than a small dot. It's a weird reason to use pointers though, but then
again , it kinda works the same way except its a little more work.

Cheers
 
M

mlimber

Joolz said:
lol - 0xc0ffee

The best was when I worked for a guy named Bob Defeo because I could
spell his name in hex (0xB0BDEFE0).
Well you're right though, the -> operator is easier to 'spot' for me
than a small dot. It's a weird reason to use pointers though, but then
again , it kinda works the same way except its a little more work.

It does kinda work the same way, but the dot operator has one major
advantage: there's no chance of dereferencing a null pointer because
you can't forget to initialize or reseat a reference. (Of course, if
the pointer is const, this criticism is mitigated.) Also, the pointer
takes up an extra chunk of otherwise unneeded space, which may be an
issue if you have many instances of the class or limited memory
requirements. Finally, those who read your code later (including you)
will have one more thing to figure out. For those reasons, I'd
discourage the practice of using -> over . just for aesthetic reasons.

Cheers! --M

PS, You could always do something really nasty and overload the ->
operator in the nested struct. That could give you the same effect, but
again, it's probably better to go with the . operator. You could always
use spaces to help set them off, e.g., a . b . c = 5;
 
M

Marcus Kwok

mlimber said:
The best was when I worked for a guy named Bob Defeo because I could
spell his name in hex (0xB0BDEFE0).

I saw in a homework assignment where they would initialize memory with
0xDEADBEEF.
 
R

red floyd

Marcus said:
I saw in a homework assignment where they would initialize memory with
0xDEADBEEF.

My favorite was 0xDEFACEABL, I've also seen 0xDEADBABE

In addition, 1337-sp33k makes it possible to have even more descriptive
identifiers (I use them for "magic numbers"/signatures in data headers).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top