Static Member Functions and inheritence

E

exits funnel

Hello,

One of the problems at the end of Chapter 14 in Bruce Eckel's thinking
in C++ reads as follows:

Create a class with two static member functions. Inherit from this
class and redefine one of the member functions. Show that the other is
hidden in the derived class.

The simple test code I wrote seems NOT to confirm this. The question
then: Is Eckel right or is my compiler right? I'm guessing the
compiler since the behavior he seems to imply makes no sense whatsoever.

-exits
 
J

John Carson

exits funnel said:
Hello,

One of the problems at the end of Chapter 14 in Bruce Eckel's thinking
in C++ reads as follows:

Create a class with two static member functions. Inherit from this
class and redefine one of the member functions. Show that the other
is hidden in the derived class.

The simple test code I wrote seems NOT to confirm this. The question
then: Is Eckel right or is my compiler right? I'm guessing the
compiler since the behavior he seems to imply makes no sense
whatsoever.

-exits


Even though he doesn't say it, I suspect that the two functions are meant to
have the same names (but different arguments).
 
V

Victor Bazarov

exits funnel said:
One of the problems at the end of Chapter 14 in Bruce Eckel's thinking
in C++ reads as follows:

Create a class with two static member functions. Inherit from this
class and redefine one of the member functions. Show that the other is
hidden in the derived class.

The simple test code I wrote seems NOT to confirm this. The question
then: Is Eckel right or is my compiler right? I'm guessing the
compiler since the behavior he seems to imply makes no sense whatsoever.

I think the exercise depends on the definition of "other". Bruce's
"Name Hiding" part of chapter 14 gives an example of class Base with
_two_ functions named "f". Perhaps he wants you to have to functions
with the same name in your base class... I suppose the text of the
exercise ought to read "Create a class with two OVERLOADED member
functions. [...]"

Anyway...

Two examples:

// One -- probably your result

struct A {
static void foo(int);
static void bar(double);
};

struct B : A {
static void foo(int);
};

int main() {
B::bar(3.14159); // OK, the "other", A::bar(double)
// is NOT hidden by B::foo(int)
}

// Two

struct A {
static void foo(int);
static void foo(double); // overloaded
};

struct B : A {
static void foo(int);
};

int main() {
B::foo(3.14159); // cannot do, the "other", A::foo(double)
// is _also_ hidden by B::foo()
}

Check out the part "Inheritance and static member functions" in the same
chapter. Items #2 and #3 in the list are of interest.

Good luck! From what I've seen Bruce Eckel's books are OK, but not free
of quirks. Find your way through them, and you're going to come out
a better C++ programmer than if you worked through a book without quirks.

Victor
 
R

Ron Natalie

exits funnel said:
Hello,

One of the problems at the end of Chapter 14 in Bruce Eckel's thinking
in C++ reads as follows:

Create a class with two static member functions. Inherit from this
class and redefine one of the member functions. Show that the other is
hidden in the derived class.

Names defined in derived classes hide the same name in base (static, nonstatic, data or function).

class Base {
public:
static void FuncA(int ) { cout << "Base::FuncA(int) called.\n"; }
static void FuncA(const char*) { cout << "Base::FuncA(const char*) called\n"; }
static void FuncB(int) { cout << "Base::FuncB(const char*) called.\n"; }
};

class Derived : public Base {
public:
static void FuncA(int) { cout << "Derived::FuncA(int) called\n"; }
};


Derived::FuncA(5); // ok, Derived::FuncA(int) called.
Derived::FuncA("foo"); // ill-formed, no overload for Derived::FuncA has no overload for const char*
// the one in Base is hidden by the definition in Derived.
Derived::FuncB(5); // ok, Base::FuncB found as nothing hides it in Derived.

Derived::FuncB
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top