473,480 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is this valid C++?

16 New Member
The following code compiles on the latest VC++, not EDG or MINGW.

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. class Base
  3. {
  4. public:
  5.     void doSomething(); 
  6. };
  7.  
  8. template <typename T>
  9. class Derived : public Base<T>
  10. {
  11. public:
  12.     using Base::doSomething; // <-- valid?
  13.     // or do you have to do this?
  14.     //using Base<T>::doSomething;
  15.  
  16.      void doSomething(int i);
  17. };
  18.  
  19.  
  20. int main()
  21. {
  22.     return 0;
  23. }
I tried to consult the standard, but couldn't find a definite answer.
Aug 14 '07 #1
10 1401
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Aug 14 '07 #2
TripleDES
16 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     Derived<int> d;
  4.     d.doSomething(); // <-- calls Base::doSomething(), 
  5.                      //need a using-declaration for this
  6.  
  7.     d.doSomething(42); // <-- calls Derived::doSomething(int)
  8.  
  9.     return 0;
  10. }
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()"?
Aug 14 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
"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:

Expand|Select|Wrap|Line Numbers
  1. void Derived::doSomething(int i)
  2. {
  3.     Base::doSomething(i);   //recover base class code
  4.     //
  5.     //TODO: Add Derived code here
  6. }
  7.  
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.
Aug 14 '07 #4
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:



  1. The doSomething in Base is public, and
  2. 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.
Aug 14 '07 #5
TripleDES
16 New Member
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.
Aug 14 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Aug 15 '07 #7
TripleDES
16 New Member
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.
Aug 15 '07 #8
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.
Aug 15 '07 #9
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.
Aug 16 '07 #10
TripleDES
16 New Member
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).
Aug 16 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

12
8139
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...
16
8073
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
1
1763
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...
7
7266
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...
23
1883
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...
3
14507
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...
0
623
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...
1
4215
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))...
1
2435
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...
10
4223
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...
0
7055
marktang
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,...
0
7106
jinu1996
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...
0
5365
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
4799
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
4501
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...
0
3013
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
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
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 ...
0
206
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.