Jay
Jay

Reputation: 79

How can I use the default C++ string constructor in Visual Studio?

So basically, I'm just messing around with classes and functions and such, and one thing I'd like to know is this:

Say I had this class, that derives off of std::string

class MyString : public std::string {
};

Because I'd like to create variables of type MyString that initializes the same way as std::string, I'd attempt to use:

using std::string::string;

However, this doesn't seem to work in Visual Studio - to VS, string() is not a member function.

This works in Code::Blocks / G++, why not Visual Studio / VC++?

Upvotes: 0

Views: 116

Answers (2)

Marco A.
Marco A.

Reputation: 43662

Works in MSVC2015. VS2013 didn't support inheriting constructors

                           2010  2012  2013
Inheriting constructors    No    No    No

while MSVC2015 does (from preview on): http://blogs.msdn.com/b/vcblog/archive/2014/11/17/c-11-14-17-features-in-vs-2015-preview.aspx

As to why you shouldn't inherit from std::string, I recommend this post: Why should one not derive from c++ std string class?

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254531

According to MSDN, inherting constructors is not supported, at least in VS2013 or earlier versions.

But you shouldn't be inheriting from std::string anyway. No good will come from that. Write non-member functions if you want to extend the functionality of an existing class.

Upvotes: 3

Related Questions