ITEM: D3981L
Question on xlC.
Question:
Question about xlC. One class has two functions and same names but
differ in the arguments that they take. The argument of one function
is a pointer to an instance of class A. For the second function, the arg
is a pointer to a function also in class A. Other compilers have
ways of sending the proper pointer to the proper funtion. How does xlC
handle this?
Response:
There is a class, we'll call it class A, that has two functions
with the same name and different arguments (overloaded functions). One
instance accepts a pointer to class B, and the second accepts a pointer to
class C. He also has class B, class C, and class D defined. Class C is
derived from class B and class D is derived from class C. He wants to know
how the compiler will resolve a call to the class A member function if he
passes a pointer to class D.
Since D is derived from C and there is not an instance of the overloaded
class A member function that accepts a pointer to class D, the overloaded
class A member function that accepts a pointer to class C will be called
(C is the base class for class D, so it implicitly calls the correct function).
Following is a code sample that demonstrates this:
\#include \
class A;
class B;
class C;
class D;
class B
{
public:
int func()
{
cout \<\< "func in B called" \<\< endl;
return( 1 );
}
};
class C: public B
{
public:
int func()
{
cout \<\< "func in C called" \<\< endl;
return( 4 );
}
};
class D: public C
{
public:
int func()
{
cout \<\< "func in D called" \<\< endl;
return( 5 );
}
};
class A
{
public:
int overloaded_func( class B *bptr )
{
bptr->func();
cout \<\< "func1 for class B called" \<\< endl;
return( 2 );
}
int overloaded_func( class C *cptr )
{
cptr->func();
cout \<\< "func1 for class C called" \<\< endl;
return( 3 );
}
};
main()
{ class A atype;
class B btype;
class C ctype;
class D dtype;
atype.overloaded_func(&btype);
atype.overloaded_func(&ctype);
atype.overloaded_func(&dtype);
Support Line: Question on xlC. ITEM: D3981L
Dated: October 1993 Category: N/A
This HTML file was generated 99/06/24~13:30:57
Comments or suggestions?
Contact us