🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Balasubramanian Tue, 20 Feb 1996 18:51
pointer conversions from the null pointer constant
#3388
Author: Balasubramanian
Date: Tue, 20 Feb 1996 18:51
32 lines
1625 bytes
Given the following:

class A {};
class B : public A {};
class C : public B {};

void foo(A*) { ... }

void foo(C*) { ... }

int main() {foo(0); return 0;}

How is the call to foo() in main() to be resolved? It seemed obvious
to me that the call should be ambiguous but some compilers resolve
it as a call to foo(C*) (while others reject it as ambiguous.)

Is the conversion of the null pointer constant to C* 
better than its conversion to A*? I couldn't find anything
in the ARM or the draft standard (May 1995) to support this 
interpretation.

Thanks.


	- balu
---
[ To submit articles: Try just posting with your newsreader.  If that fails,
                      use mailto:std-c++@ncar.ucar.edu
  FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std.c++-request@ncar.ucar.edu 
]
Re: pointer conversions from the null pointer
#3358
Author: Steve Clamage
Date: Tue, 20 Feb 1996 21:32
44 lines
1854 bytes
In article 8J1@netcom.com, balu@netcom.com (Balasubramanian) writes:
>Given the following:

>class A {};
>class B : public A {};
>class C : public B {};

>void foo(A*) { ... }

>void foo(C*) { ... }

>int main() {foo(0); return 0;}

>How is the call to foo() in main() to be resolved? It seemed obvious
>to me that the call should be ambiguous ...

It seems obvious to me too. The null pointer constant is an exact match
for any pointer, so there is no reason to prefer either version of foo.

There is a preference associated with class hierarchies. For example:
	class X { ... };
	class Y : public X { ... };
	class Z : public Y { ... };
	void foo(X*);
	void foo(Y*);

	Z* pz = ...;
	foo(pz);

A Z* can be converted to either a Y* or an X*, but Y* is preferred. This
case does not apply to null pointer constants, however, since a null
pointer constant does not have type Z*.

---
Steve Clamage, stephen.clamage@eng.sun.com



[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads