By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

Static Member Functions and inheritence

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"exits funnel" <ex*********@NOSPAMyahoo.com> wrote in message
news:3F**************@NOSPAMyahoo.com
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).
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2

P: n/a
"exits funnel" <ex*********@NOSPAMyahoo.com> wrote...
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
Jul 22 '05 #3

P: n/a

"exits funnel" <ex*********@NOSPAMyahoo.com> wrote in message news:3F**************@NOSPAMyahoo.com...
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.