Conversions and overload resolution

  • Thread starter Peteris Krumins
  • Start date
P

Peteris Krumins

Hello!

While experimenting with C++ language, I came across intersting
behavior.
Suppose class Derived privately inherits from class Base.
Derived cannot be implicitly converted to Base because it is
inherited privately (Derived IS-NOT-A Base).
Now I add two overloaded functions, one which takes a pointer to
Base and the other which just takes ellipsis.

Example:

class Base { };
class Derived : private Base { };

void test(Base *ptr);
void test(...);

//...

Now if I call the function test() and pass a pointer to a Derived
class,
function test(Base *ptr) get's selected as if Derived could be
converted to
Base and then compiler errors that Derived cannot be converted to
Base.

Example:
test(new Derived);


Why doesn't the compiler select test(...) as the best match?


P.Krumins
 
P

Philippe Amarenco

Peteris Krumins said:
Hello!

While experimenting with C++ language, I came across intersting
behavior.
Suppose class Derived privately inherits from class Base.
Derived cannot be implicitly converted to Base because it is
inherited privately (Derived IS-NOT-A Base).
Now I add two overloaded functions, one which takes a pointer to
Base and the other which just takes ellipsis.

Example:

class Base { };
class Derived : private Base { };

void test(Base *ptr);
void test(...);

//...

Now if I call the function test() and pass a pointer to a Derived
class,
function test(Base *ptr) get's selected as if Derived could be
converted to
Base and then compiler errors that Derived cannot be converted to
Base.

Example:
test(new Derived);


Why doesn't the compiler select test(...) as the best match?

name resolution is done before checking access rights.

the same goes for this:

class A { public: void foo(); };
class B : public A { private: void foo(); };

B x; x.foo(); // error, it selected B::foo.
 

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,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top