derived class objects in base class static function

Discussion in 'C++' started by Chris Forone, Mar 18, 2008.

  1. Chris Forone

    Chris Forone Guest

    hello group,

    is it possible to use derived class objects in base class static function?

    example:

    #include <iostream>

    class b;

    class a
    {
    public:
    a() {}
    virtual ~a() {}

    static void Init() {b thing; thing.Do();} // compile
    // error
    };

    class b : public a
    {
    public:
    b() {}
    virtual ~b() {}

    void Do() {std::cout << "hello?" << std::endl;}
    };

    int main()
    {
    a::Init();
    }

    thanx & hand, chris
     
    Chris Forone, Mar 18, 2008
    #1
    1. Advertising

  2. Chris Forone

    Ian Collins Guest

    Chris Forone wrote:
    > hello group,
    >
    > is it possible to use derived class objects in base class static function?
    >
    > example:
    >
    > #include <iostream>
    >
    > class b;
    >
    > class a
    > {
    > public:
    > a() {}
    > virtual ~a() {}
    >
    > static void Init() {b thing; thing.Do();} // compile
    > // error


    Declare the function here, define it after class b.

    --
    Ian Collins.
     
    Ian Collins, Mar 18, 2008
    #2
    1. Advertising

  3. Chris Forone

    Chris Forone Guest

    Ian Collins schrieb:
    > Chris Forone wrote:
    >> hello group,
    >>
    >> is it possible to use derived class objects in base class static function?
    >>
    >> example:
    >>
    >> #include <iostream>
    >>
    >> class b;
    >>
    >> class a
    >> {
    >> public:
    >> a() {}
    >> virtual ~a() {}
    >>
    >> static void Init() {b thing; thing.Do();} // compile
    >> // error

    >
    > Declare the function here, define it after class b.
    >

    thanx, the example works. the original code is with headers &
    implementation separated and therefor not as simple...
     
    Chris Forone, Mar 18, 2008
    #3
  4. Chris Forone

    Chris Forone Guest

    Chris Forone schrieb:
    > Ian Collins schrieb:
    >> Chris Forone wrote:
    >>> hello group,
    >>>
    >>> is it possible to use derived class objects in base class static
    >>> function?
    >>>
    >>> example:
    >>>
    >>> #include <iostream>
    >>>
    >>> class b;
    >>>
    >>> class a
    >>> {
    >>> public:
    >>> a() {}
    >>> virtual ~a() {}
    >>>
    >>> static void Init() {b thing; thing.Do();} // compile
    >>> // error

    >>
    >> Declare the function here, define it after class b.
    >>

    > thanx, the example works. the original code is with headers &
    > implementation separated and therefor not as simple...

    ok, done! ive put the #include of derived class directly after #include
    of base class in base class implementation. dont know if this is best
    solution... thanx a lot, chris
     
    Chris Forone, Mar 18, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran
    Replies:
    4
    Views:
    837
    Micah Cowan
    Jan 12, 2005
  2. Replies:
    1
    Views:
    419
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    405
    Victor Bazarov
    May 23, 2007
  4. George2
    Replies:
    0
    Views:
    372
    George2
    Mar 17, 2008
  5. Lionel B
    Replies:
    8
    Views:
    356
    James Kanze
    Feb 5, 2009
Loading...

Share This Page