reach a function in a sub class?

Started by
5 comments, last by kappa 20 years, 10 months ago
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]
Advertisement
Cfoo.foo::bar() maybe?
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.
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]
quote:Original post by Anonymous Poster
Cfoo.foo::bar() maybe?


Only if foo::bar() is defined as static, which it isn''t in this case.
foobar::foo myfoo;
myfoo.bar();
cool thank you

This topic is closed to new replies.

Advertisement