Archived

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

reach a function in a sub class?

This topic is 5306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need help how would I reach bar()? is it even possible?
class foobar
{
     class foo
     {
         void bar(){}
     };
};

int main()
{
     foobar Cfoo;
return 0;
}
[edited by - kappa on June 2, 2003 12:15:42 PM]

Share this post


Link to post
Share on other sites
You have to have an instance of class foo created somewhere, probably inside foobar...


class foobar
{
public:
class foo
{
void bar() {}
}

foo foo_;
};

int main()
{
foobar myFoobar;
myFoobar.foo_.bar();
return 0;
}


The way your code was written, you have only defined the classes foobar and foo and created an instance of foobar, but there''s no instance of foo anywhere. Remember, defining a class does not create an instance of it.

Share this post


Link to post
Share on other sites
You need to declare an instance of the nested class to be able to access it, so you could do


class foobar
{
class foo
{
void bar() {}
} m_Nested;
};

int main()
{
foobar Cfoo;

m_Nested.bar();

return 0;
}


Alternatively, you could make bar static as long as it doesn't access any non-static data in class foo, so this might also work


class foobar
{
class foo
{
static void bar() {}
} m_Nested;
};

int main()
{
foobar::foo::bar();

return 0;
}


EDIT: Beaten! Arrgh.

[edited by - AikonIV on June 2, 2003 12:20:46 PM]

Share this post


Link to post
Share on other sites