Automatic conversion to base class when calling constructor.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Belebele

    Automatic conversion to base class when calling constructor.

    I would like to have the compiler bind a Base class const reference to
    a temporary object of a Derived class when calling another class
    constructor. Please see the code below:

    class Base {};

    class Derived: public Base {};

    class Foo {
    public:
    Foo(Base const& );

    void bar();
    };

    int main()
    {
    Foo foo(Derived());

    foo.bar();
    }



    One compiler gives me the following error:
    error: expression must have class type foo.bar();

    Another compiler also complains:
    error: request for member `bar' in `foo', which is of non-aggregate
    type `Foo ()(Derived (*)())'

    Any idea why?

  • Piyo

    #2
    Re: Automatic conversion to base class when calling constructor.

    Belebele wrote:
    I would like to have the compiler bind a Base class const reference to
    a temporary object of a Derived class when calling another class
    constructor. Please see the code below:
    >
    class Base {};
    >
    class Derived: public Base {};
    >
    class Foo {
    public:
    Foo(Base const& );
    >
    void bar();
    };
    >
    int main()
    {
    Foo foo(Derived());
    >
    foo.bar();
    }
    >
    >
    >
    One compiler gives me the following error:
    error: expression must have class type foo.bar();
    >
    Another compiler also complains:
    error: request for member `bar' in `foo', which is of non-aggregate
    type `Foo ()(Derived (*)())'
    >
    Any idea why?
    >
    This is the reason why you see that error.



    HTH

    Comment

    Working...