private inheritance from std::string error

H

Heiko Hund

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
 
I

Ivan Vecerina

Heiko Hund said:
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::eek:perator 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
 
H

Heiko Hund

Ivan said:
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::eek:perator 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
 
M

msalters

Heiko said:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top