Automatic conversion to base class when calling constructor.

B

Belebele

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?
 
P

Piyo

Belebele said:
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.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19

HTH
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top