By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,636 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

private inheritance from std::string error

P: n/a
Hi,

I do not understand the deeper reason for the following compiler error

$ g++ test.cpp
test.cpp: In function `int main()':
test.cpp:41: error: `std::basic_string<char, std::char_traits<char>,
std::allocator<char> >' is an inaccessible base of `Path'

I'm using Debians

$ g++ --version
g++ (GCC) 3.3.5 (Debian 1:3.3.5-8)
$ dpkg -l libstdc++5
ii libstdc++5 3.3.5-8 The GNU Standard C++ Library v3

Here the source code that produces the problem:

#include <string>

class Path : private std::string
{
public:
Path(std::string const & p)
:std::string(p)
{}

Path(Path const & rhs)
:std::string(rhs)
{}

operator std::string() const
{
return static_cast<std::string>(*this);
}

operator std::string const() const
{
return static_cast<std::string const>(*this);
}

operator std::string const &() const
{
return static_cast<std::string const &>(*this);
}

operator char const *() const
{
return c_str();
}
};

int main()
{
Path p("foo");
Path p2(p);
char const * c(p);

std::string s(p); // <- error

return 0;
}

Any enlightenment is highly appreciated.

Thanks
Heiko
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Heiko Hund" <he***@ist.eigentlich.net.invalid> wrote in message
news:cu**********@schlund.de...
Hi,

I do not understand the deeper reason for the following compiler error

$ g++ test.cpp
test.cpp: In function `int main()':
test.cpp:41: error: `std::basic_string<char, std::char_traits<char>,
std::allocator<char> >' is an inaccessible base of `Path'

I'm using Debians

Posting your code to the Comeau compiler,
http://www.comeaucomputing.com/pcgi-bin/compiler.cgi,
gives the following warnings:

"ComeauTest.c", line 14: warning: "Path::operator std::string() const" will
not be called for implicit or explicit conversions

I guess the base class conversion takes priority over
whater operator you declare.
How about using a private member instead?

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #2

P: n/a
Ivan Vecerina wrote:
Posting your code to the Comeau compiler,
http://www.comeaucomputing.com/pcgi-bin/compiler.cgi,
gives the following warnings:

"ComeauTest.c", line 14: warning: "Path::operator std::string() const"
will not be called for implicit or explicit conversions

I guess the base class conversion takes priority over
whater operator you declare.
Hmm, I wonder why. My conversion operator should be more specific than a
implicit cast to a base class. Especially since private inheritance
expresses a 'has a' relation, afaik.
How about using a private member instead?


Though about it, but wanted to avoid simple wrapper methods to access
std::string functionality. I think I'll have to go that way now. A pity!

Thanks again
Heiko
Jul 23 '05 #3

P: n/a

Heiko Hund wrote:
#include <string>

class Path : private std::string
{
public:
Path(std::string const & p)
:std::string(p)
{}

Path(Path const & rhs)
:std::string(rhs)
{}

operator std::string() const
{
return static_cast<std::string>(*this);
}

operator std::string const() const
{
return static_cast<std::string const>(*this);
}
};


Derived-to-base conversions are selected over user-defined
conversions, and access checking is performed after the
conversion sequence is selected. That's why your private base
class hides the user-defined conversion, even though it's
private.

HTH,
Michiel Salters

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.