4

I have seen this example from the book:

class Test: private std::string
{
public:
    Test():std::string("How?")
    {
    }
};

int main(int argc, char** argv)
{
    Test t;

    std::cout<<(std::string&)t<<std::endl;

    return 0;
}

I don't know how did it printed "how" when i typecasted the class name? is it because of operators? but i know that when you do private inheritance, the public and protected variables and methods will be considered "private" outside.

So my question is, how did it exactly printed "How" ?

edit :

so who is holding the string value "How" and how was it printed? Because it was printed by typecasting.

1

2 Answers 2

6

This illustrates one of the dangers of the C-style cast : it's the only cast that ignores inheritance access specifiers. As you saw, the cast successfully dug out the std::string base reference, even though it was private. If you try this with a static_cast, it won't compile.

Edit :

The std::string subobject of t is holding "How", as you initialized it in Test's constructor. Casting to a reference to a base class serves to access the corresponding subobject.

Sign up to request clarification or add additional context in comments.

9 Comments

Well, i saw this technique from the book "C++ primer" so i kinda wondered.
@CarloBrew A book that suggests c-style casts is a bit dated, and a book that suggests inheriting from a std container is giving bad advice.
@acraig5075 depends on the context. As long as you always inherit privately if your base class is not virtual-destructible, and you use C-style cast with care and parsimony, everything's fine.
@TonyD: No, reinterpret_cast would pretend there's a string at the same address, giving undefined behaviour if it isn't a standard-layout type. An evil C cast will correctly adjust the address to refer to the base-class subobject, as static_cast would if the base class were accessible.
@MikeSeymour: I'm not saying it's better or equivalent or safe, just highlighting that it can also bypass the access specifiers as the text I quoted implied no "new-style" casts were capable of that. Quentin: suit yourself - it's mentioned now in comments anyway for anyone else pedantic enough to read that far....
|
3

The cast (std::string&)t casts t to a reference to an instance of its base class std::string. C style casts are the only casts that can cast to an inaccessible base class. The effect is the same as with an ordinary implicit conversion to an accessible base class.

6 Comments

so who is holding the string value "How" and how was it printed? Because it was printed by typecasting.
The t object has a std::string sub-object. Every base class introduces a base class sub-object.
which means that, once expression t is cast to std::string&, it will print "how" by your good ole operator<<(std::ostream&, std::string&).
@jrok but when i did the private inheritance, the public and protected functions/variables of base will be converted to private upon inheritance. Doesn't it make the operator inaccessible(including the operators which is on public) ?
You're passing a reference to std::string to the iostream << operator.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.