C++ const correctly use

55 pts.
Tags:
C++
Visual Studio
Visual Studio Test Edition
I have this sample code ( below ), whatever1() method works with no problem, whatever2() is similar but I must force const_char to make it compile, although I consider as whatever1() method is not needed, whatever2() would be not needed either. 
My question is, how can I modify add() method to make both compile or how must I correctly call buffer.add() in whatever2() without forcing const_cast? add() method is not modifying item, so const_cast is unnecessary. Which is the correct or suitable form? 
SAMPLE CODE ==========
template <class Item>
class Buffer
{
public:
    Item *      _pItems;
    int         _nItems;
    // ... constructor / destructors etc
    void    add( const Item & item ) // or maybe Item const & item
    {
        _pItems[_nItems++] = item;
    }
};
class MyClass
{
public:
    // data
};
void    whatever1( const MyClass & item )
{
    Buffer<MyClass>        buffer;
    buffer.add( item );  // WORKS, no problem
}
void    whatever2( const MyClass & item )
{
    Buffer<MyClass *>        buffer; // NOW with pointers to MyClass
    //buffer.add( item );  // ERROR: 'Buffer<Item>::add' : cannot convert parameter 1 from 'const MyClass' to 'MyClass *const &'
    buffer.add( const_cast<MyClass *>( &item ) );  // forcing const_cast WORKS
}


Software/Hardware used:
visual studio

Answer Wiki

Thanks. We'll let you know when a new response is added.

The use of the <b>const</b> qualifier can be complicated in some cases, but in this case I don’t think the problem is directly related to it.

In <i>whatever2 </i>you are using the template with pointers to MyClass (<<i>MyClass *</i>>), but the parameter (<i>item</i>) is not a pointer to <i>MyClass</i>. Although it is declared in the function as “<i>const MyClass <b>&</b> item</i>”, inside the function it is not a pointer, it is just a modifiable <i>MyClass </i>object.

I think <i>cons_cast </i>is making it work because in addition to removing the <i>const </i>attribute it is implicitly casting ‘<i>&item</i>’ to <i>’MyClass *</i>’.

If that is correct, then, this should also compile:

<pre>buffer.add( (MyClass *)&item );</pre>

BTW, this is complicated to understand when <i>Item </i>is being substituted by <i><MyClass *></i>:

<pre>add( const Item & item )</pre>

Don’t you think ? ;-)

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Thetonatiuh
    yes, you are right buffer.add( (MyClass *)&item ); // This compiles correcty but my point is related to the const thing, let me clarify with a 3rd example in the whatever3 method:
    void    whatever3( const MyClass & item )
    {
        Buffer<MyClass *>       buffer; // with pointers to MyClass like whatever2()
        const MyClass *         pItem = &item; // This is working great, no problem, from now we are using pItem all the time so we are already working with pointer 
    
        //buffer.add( pItem );  // ERROR: 'Buffer<Item>::add' : cannot convert parameter 1 from 'const MyClass *' to 'MyClass *const &'
        
        buffer.add( const_cast<MyClass *>( pItem ) ); // forcing const_cast WORKS    
        
        buffer.add( (MyClass *)pItem ); // as suggested by "Carlosdl" this also works but notice that in fact this is removing the "const" of pItem definition, so this is doing similar to const_cast
        
        buffer.add( (MyClass * const &)pItem ); // this is working, because this is what compiler is expecting, but again I had to remove the const of the pointer. 
    }
    The point is, I know the contens of item will be not changed anywhere, so const is correct, i must not remove it On the other hand I always need to remove the "const" part to make it compile. So the question is, maybe there is a way I must modify the specification of method: void add( const Item & item ) in the Buffer class, or maybe i need to add a second add() method to be able to compile in both situations? with reference and with pointer, without the need of forcing cast or removing the const thing?
    55 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following