473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

"inheriting" constructors

Hi,

I'd like to define a class that should behave as much as posible
like std::string, but that has some small additional property:

class ExtendedString: public std::string {
public:
void someExtendedFunctionality(void) {}
};

As new classes do not inherit the constructors from its base, is
it true that I would have to define counterparts for all std::string
constructors that I may want to use to construct my derived class?

class ExtendedString: public std::string {
public:
void someExtendedFunctionality(void) {}
ExtendedString(const char *data): std::string(data) {}
ExtendedString(const std::string &data): std::string(data) {}
ExtendedString(void): std::string() {}
// ......
};

Or is there some smarter way to tell the compiler that the constructors
of std::string may be used right away, saving all the typing effort?

Thanks for any tips,

Christof
Jul 18 '07 #1
2 2210
Christof Warlich wrote:
Hi,

I'd like to define a class that should behave as much as posible
like std::string, but that has some small additional property:

class ExtendedString: public std::string {
public:
void someExtendedFunctionality(void) {}
};

As new classes do not inherit the constructors from its base, is
it true that I would have to define counterparts for all std::string
constructors that I may want to use to construct my derived class?

class ExtendedString: public std::string {
public:
void someExtendedFunctionality(void) {}
ExtendedString(const char *data): std::string(data) {}
ExtendedString(const std::string &data): std::string(data) {}
ExtendedString(void): std::string() {}
// ......
};

Or is there some smarter way to tell the compiler that the constructors
of std::string may be used right away, saving all the typing effort?
a) You can use templated constructors:

ExtendedString() : std::string() {}

template < typename A >
ExtendedString( A a ) : std::string( a ) {}

With std::string, I think you need to go up to 4 arguments.
b) It appears that your ExtendedString has no additional data members and
that the functionality provided by

void someExtendedFunctionality(void) {}

could as well be provided by a free-standing function. If so, a
free-standing functions is probably more appropriate. Maybe, you can even
come up with a generic implementation working on character-ranges. In that
case, you would have a more flexible addition to your library with less
code.
c) Many people frown upon public derivation from container classes. In my
opinion, it's not a deadly sin; but you should be aware of the possible
pitfalls:

1) Keep in mind that the destructor of std::string is non-virtual.

2) Note that functions like

std::string some_func( std::string const & str );

will happily accept ExtendedString objects as arguments, but they
will return std::string output. There are situations, where this
can get tricky.
Best

Kai-Uwe Bux
Jul 18 '07 #2
Kai-Uwe Bux schrieb:
a) You can use templated constructors:

ExtendedString() : std::string() {}

template < typename A >
ExtendedString( A a ) : std::string( a ) {}

With std::string, I think you need to go up to 4 arguments.
Thanks, that was the type of idea I was looking for. Although
it's a pity that it still needs one definition for each number
of parameters that shall be supported ...
c) Many people frown upon public derivation from container classes. In my
opinion, it's not a deadly sin; but you should be aware of the possible
pitfalls:

1) Keep in mind that the destructor of std::string is non-virtual.

2) Note that functions like

std::string some_func( std::string const & str );

will happily accept ExtendedString objects as arguments, but they
will return std::string output. There are situations, where this
can get tricky.
But even more thanks for pointing out the pitfalls!
Jul 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
12
by: relaxedrob | last post by:
Hi All! I have a page with with the following style information: <link rel="stylesheet" type="text/css" href="/eEmployment/eTech.css" /> <style type="text/css"> DIV.Application {...
12
by: Ola Johansson | last post by:
Can anyone explain to me a meningfull use of the "new" keyword (Shadows in VB). I think i understand how it works fully but i cant figure out why you would use it. Why would you want to declare...
6
by: Jose Perez | last post by:
Dear All, We are experiencing a problem with web.config and the fact that it's settings are being "inherited" other applications. Our default website is configured to run "MyApplication1". The...
5
by: Alias | last post by:
Hi - I'm trying to implement a custom RoleProvider based on the SqlRoleProvider. I keep receiving a an error that it can't load type 'MyRoleTest.MyRoleProvider' when trying to load my...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
1
by: Adam Nielsen | last post by:
Hi again, I've got another question about template specialisation. I would like to declare some data types in the main template (the "base class") but I would then like to extend the behaviour...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
1
by: Arthur Dent | last post by:
Hello all... I have a method which returns a KeyValuePair(Of Long, String). I would like to make an alias for that, so instead of typing KeyValuePair(Of Long, String) everywhere I could just...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.