The following code compiles on the latest VC++, not EDG or MINGW. -
template <typename T>
-
class Base
-
{
-
public:
-
void doSomething();
-
};
-
-
template <typename T>
-
class Derived : public Base<T>
-
{
-
public:
-
using Base::doSomething; // <-- valid?
-
// or do you have to do this?
-
//using Base<T>::doSomething;
-
-
void doSomething(int i);
-
};
-
-
-
int main()
-
{
-
return 0;
-
}
I tried to consult the standard, but couldn't find a definite answer.
10 1401
What are you trying to do???
In Derived there is a method:
void doSomething(int i);
which hides Base::doSomething().
If you are using polymorphism, this is an error since Derived::doSomething() will never be called using a Base pointer or reference.
On the other hand, if you are doing object-based programming, then this would be OK so long as Derived::doSomething() did not have to internally call Base::doSomething() to recover the the code in Base that is being hidden by Derived.
What are you trying to do???
In Derived there is a method:
void doSomething(int i);
which hides Base::doSomething().
If you are using polymorphism, this is an error since Derived::doSomething() will never be called using a Base pointer or reference.
Actually the code is not really supposed to do anything, I just tried to make my code example as concise as possible. The intent is to grant access to the doSomething() method that would otherwise be hidden from a Derived object, like so: - int main()
-
{
-
Derived<int> d;
-
d.doSomething(); // <-- calls Base::doSomething(),
-
//need a using-declaration for this
-
-
d.doSomething(42); // <-- calls Derived::doSomething(int)
-
-
return 0;
-
}
I guess I should have been more specific. My question is about the syntax of the using-declaration: Is it valid C++ to write "using Base::doSomething()" without supplying a template parameter or do you have to write "using Base<T>::doSomething()"?
"using Base::doSomething()" without supplying a template parameter or do you have to write "using Base<T>::doSomething()"?
You use the one that compiles.
just tried to make my code example as concise as possible. The intent is to grant access to the doSomething() method that would otherwise be hidden from a Derived object,
You can code this way. It's not recommended and can set you up for an ambiguous call. But I have seen this done: -
void Derived::doSomething(int i)
-
{
-
Base::doSomething(i); //recover base class code
-
//
-
//TODO: Add Derived code here
-
}
-
The problem is that it is not clear if the call to Base::doSomething() is a pre-condition or a post-condition. That is, do you call it before the Derived code or after the Derived code. That ambiguity points to a design weakness. Any weakness should be fixed in the design so you don't have to do this.
RRick 463
Recognized Expert Contributor
What is the error message you are getting. Is it the same for both compilers? Templates have changed a lot in the last few years and templates are the root of many syntax problems. For example typename is a new construct and some compilers can't handle it.
The doSomething's in both classes are different. The base class and derived class define separate/different doSomething methods (based on the parameters defined). This is perfectly legal.
From the derived class you can access the base class and derived class doSomething. You don't need to worrry about defining anything extra in the derived class because: - The doSomething in Base is public, and
- You inherit the base class as public.
You can get rid of all the "using" stuff in your code. Using only affects namespaces, not methods.
Thanks for your reply. I realize, of course, that my example code demonstrates questionable design. In general I would not use public inheritance unless I intend to use the base class polymorphically, and certainly not if it doesn't have a single virtual function/dtor.
Even if my question was only related to the syntax of the using-declaration, you bring up some good points related to class design that I believe more people than me can learn from.
In general I would not use public inheritance unless I intend to use the base class polymorphically,
That's not the reason for public inheritance. Public inheritance is used when you want the interface of the base class to become part of the interface of the derived class.
Polymorphism is usually implemented using private inheritance. The derived class private methods override the base class private virtual methods. The base class public methods are the interface to the hierarchy and they are not supposed to be virtual.
Polymorphism is usually implemented using private inheritance.
I agree that what you describe is a form of polymorphism, but I disagree that it is the most common form. If you use private inheritance, only the members of Derived (and friends) can use the class polymorphically in place of a Base class.
Inheriting publicly with the Derived class overriding Base's virtual functions we can use a Base* polymorphically through late binding, and I'd say that's how polymorphism is usually implemented.
RRick 463
Recognized Expert Contributor
First of all, polymorphism is only supported through public and protected inheritance. Private things remain private and can not be expanded, accessed, or polymorphized(?word?) from derived classes.
This is why I make my object's methods and data protected instead of private. This allows the derived classes access to the internals. I agree this could be dangerous, but I make a distinction between public access of an object and derived objects. Derived objects are suppose to know what they do, whereas the public must be "protected" from the internals. In my designs, the only thing that is private are things that I definitely don't want the derived class to mess with.
The discussion here seems to be whether public vrs protected inheritance is correct/normal/the best. I think it depends on the use. If you want all of the base class functionality to be publicly available, then inherit it publicly. Otherwise, it must be inherited as protected, and the derived class releases specific functionality itself.
The second approach is what shows up mostly in the literature (i.e. book I have read), but in practice I've seen both. Sometimes I lean on public inheritance simply through laziness and time constraints, but the choice is really application specific.
RRick 463
Recognized Expert Contributor
I went back and took a second look and realized I was definitely confused in my reply. For the record, you can inherit public, protected or private. Private inheritance tends to be pretty rare (at least from what I've seen) but I suppose it has its uses.
I still stand by my last two paragraphs. Here, I assume the main difference and discussion is between public verus protected or private access to an object.
I went back and took a second look and realized I was definitely confused in my reply. For the record, you can inherit public, protected or private. Private inheritance tends to be pretty rare (at least from what I've seen) but I suppose it has its uses.
Private inheritance is (or should be) used if you want to use your base class as an implementation detail in your subclass, whereas public inheritance is used to model IS-A, per the Liskov Substitution Principle. This is a prerequisite for being able to use the subclass polymorphically in place of a base. (If you inherit privately you have to explicitly grant friendship to every function and class that wants the same behavior).
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: lawrence |
last post by:
I have a string which I want to send to eval(). How can I test it
ahead of time to make sure it is valid code? I don't want to send it
to eval and get parse errors. I want to do something like...
|
by: siliconmike |
last post by:
Hi, I'm looking for a reliable script that would connect to a host and
somehow determine whether an email address is valid.
since getmxrr() only gets the mx records..
Links/pointers ?
Mike
|
by: Anna |
last post by:
Hi all.
I have probably a rather stupid question.
If there is an HTML document, XML-formed using JTidy,
is there any tool to convert it to valid XHTML?
I.e. so that all the tags and attribute...
|
by: JR |
last post by:
Hey all,
I have read part seven of the FAQ and searched for an answer but can
not seem to find one.
I am trying to do the all too common verify the data type with CIN.
The code from the FAQ...
|
by: James Aguilar |
last post by:
Someone showed me something today that I didn't understand. This doesn't
seem like it should be valid C++. Specifically, I don't understand how the
commas are accepted after the function...
| |
by: Chris |
last post by:
Hi,
In C# I tried to save a file from a generated file name.
Just before launching the dialog I check for a valid file
name to be sure.
There for I used the method ValidateNames from the save...
|
by: QA |
last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003
using SQL Server 2005
I am getting the following error:
Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
|
by: Robert Morgan |
last post by:
|I'm trying to run a query on a database using php and postgres functions
||<?php
db_connect();
$stat = pg_exec($connstr,"SELECT WSID from tblWorkstation ");
while ($row = pg_fetch_rows($stat))...
|
by: illegal.prime |
last post by:
Hey all, I have an app, that could take two numbers of any type of
numerical type int, long, double, float, uint, ulong, etc. I want to
check that the numbers are part of a range that I consider...
|
by: SpreadTooThin |
last post by:
Hi I'm writing a python script that creates directories from user
input.
Sometimes the user inputs characters that aren't valid characters for a
file or directory name.
Here are the characters...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
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...
| |