473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Conversions and overload resolution

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

Sep 2 '05 #1
1 1161
"Peteris Krumins" <pe*************@gmail.com> writes:
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.

--
Philippe Amarenco, aka Phix
epita 2007 - LSE - EpX
"if new true friend not protected for explicit private union, break
case and try using this." -- Nathan Myers, longest c++ sentence.
Sep 2 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Vladimir Granitsky | last post by:
Hi guys, Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!? Am I right that C# compiler does wrong...
0
by: Eph0nk | last post by:
Hi, I get an overload resolution failed error (Overload resolution failed because no accessible 'New' can be called without a narrowing conversion), and I can't seem to find a lot of relevant...
2
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
3
by: Old Wolf | last post by:
Here is a simplified version of a post I saw on another NG: struct Base {}; struct Derived: Base {}; struct ConstPtr { //operator const Derived * (); // F1 operator const Derived *...
3
by: Christof Nordiek | last post by:
Given the following code Is there any way to call one of the Bar methods. using System; class Program { static void Main() { Foo<string, stringfoo = new Foo<string,string>();
2
by: adsci | last post by:
Hello! Im posting this to c.l.c++ AND win32 because i dont know if this is a MS Compiler Issue or not. here we go: <code> class MyCString
2
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
2
by: zbigniew | last post by:
Can someone explain me how overload resolution works in C++? For example if I have function void f(char, int); and I will call f('A', 3.1) or f(1.5, 3.1F) what would be the result? Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.