473,499 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Member Functions and inheritence

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
3 2601
"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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
4357
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
4
7388
by: 0to60 | last post by:
I don't know if I have that terminology right, but does anyone know if static member functions (or free standing functions for that matter) are any less overhead than actual member functions that...
30
3426
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
4568
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
6
5817
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
15
6553
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
11
3793
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
7
1756
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0;...
10
32176
by: shanknbake | last post by:
I'm getting the following compile-time error: error C2352: 'Person::getCount' : illegal call of non-static member function Here is my getCount function declaration:...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.