Archived

This topic is now archived and is closed to further replies.

Karg

access problems with nested classes

Recommended Posts

Can a nested class access members of the class it is in? I want to basically do the following:
  
class ParticleSystem
{
float timeDelta;

   struct ageParticles
   {
	bool operator() ( Particle & p )
	{
		return (p.age += timeDelta) > p.lifetime;
	}
   };
};
  
but I can''t access timeDelta. The error I get is: member from enclosing class is not a type name, static, or enumerator using VC++6 Karg

Share this post


Link to post
Share on other sites
Only new types carry over (That includes enums).

And, well, static stuff can be accessed from anything as long as it''s public.

So, no, it doesn''t work that way. By nesting the class, you limited the scope of the class'' typename. (You COULD create a class with the same name as the nested one outside the parent class without any trouble.) You haven''t given it access to anything at all; it''s just another class in all other respects.

Share this post


Link to post
Share on other sites
Yes,

  
class ParticleSystem
{
float timeDelta;
struct ageParticles
{
bool operator() ( Particle & p )
{
return (p.age += timeDelta) > p.lifetime;
}
};
friend struct ageParticles;
};

Share this post


Link to post
Share on other sites
That won''t work, since it would require the nested class to have an implicit this-pointer to the enclosing class. This is how Java handles inner classes.
You will have to do something like this:

  
class ParticleSystem
{
float timeDelta;
struct ageParticles
{
ageParticles( ParticleSystem& pSystem ) : particleSystem( pSystem )
{;}
const ParticleSystem& particleSystem;

bool operator() ( Particle & p )
{
return (p.age += particleSystem.timeDelta) > p.lifetime;
}
};
};

The friend isn''t necessary.


"It is precisely because it is fashionable for Americans to know no science, even though they may be well educated otherwise, that they so easily fall prey to nonsense. They thus become part of the armies of the night, the purveyors of nitwittery, the retailers of intellectual junk food, the feeders on mental cardboard, for their ignorance keeps them from distinguishing nectar from sewage."
Isaac Asimov

Share this post


Link to post
Share on other sites