Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++Goto page 1, 2  Next

Smart Pointer problem

 
Jump to:  
 
Saurabh Gupta
PostPosted: Thu Jun 26, 2008 7:22 pm    Post subject: Smart Pointer problem
       
{ Please avoid too long lines; it's best to limit your text to
70 columns or so, less than 80 at least. In addition, please consider
using a charset that fits the English environment better, such as
US-ASCII or ISO-8859-*. -mod }

We have our smart pointer implementation. But the operator -> is
creating problem in case of a const pointer. It's always returning a
non-const pointer, therefore, we are able to call a non const function
on a const pointer. How can we overcome this problem?
Smart pointer code.

/**
* Returns the raw pointer for use in calling member functions.
*/
T *operator->() const
{
return m_p;
}

Sample Code:

class Test {
public:
void ChangeData(int a, int b)
{
X = a;
Y = b;
}
private:
int x;
int y;
};
typedef CC::CSmartPointer<Test> TestPtr;

Void TestData(const TestPtr& test)
{
// here. We should get the compilation error. But it’s
working fine due to behavior of
//the Smart Pointer operator -> .

test->ChangeData(x, y);
}


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Maciej Sobczak
PostPosted: Fri Jun 27, 2008 8:07 pm    Post subject: Re: Smart Pointer problem
       
On 26 Cze, 21:22, Saurabh Gupta <subscription.gu...@gmail.com> wrote:

Quote:
We have our smart pointer implementation. But the operator -> is
creating problem in case of a const pointer.

There is a difference between a const pointer and a pointer to const.

Consider instantiating your smart pointer template with const T
(instead of just T) to get the desired behaviour.

Something like:

typedef CC:CSmartPointer<const Test> ConstTestPtr;

instead of:

Quote:
typedef CC::CSmartPointer<Test> TestPtr;

--
Maciej Sobczak * LINK * LINK

[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Greg Herlihy
PostPosted: Fri Jun 27, 2008 8:10 pm    Post subject: Re: Smart Pointer problem
       
On Jun 26, 12:22 pm, Saurabh Gupta <subscription.gu...@gmail.com>
wrote:
Quote:
We have our smart pointer implementation. But the operator -> is
creating problem in case of a const pointer. It's always returning a
non-const pointer, therefore, we are able to call a non const function
on a const pointer. How can we overcome this problem?
Smart pointer code.

/**
* Returns the raw pointer for use in calling member functions.
*/
T *operator->() const
{
return m_p;
}

The solution is straightforward: declare two operator->()'s - one for
const smart pointer objects and the other for non-const smart pointer
objects:

const T* operator->() const
{
return m_p;
}

T* operator->()
{
return m_p;
}

Greg


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Alf P. Steinbach
PostPosted: Fri Jun 27, 2008 8:14 pm    Post subject: Re: Smart Pointer problem
       
* Saurabh Gupta:
Quote:

We have our smart pointer implementation. But the operator -> is
creating problem in case of a const pointer. It's always returning a
non-const pointer, therefore, we are able to call a non const function
on a const pointer. How can we overcome this problem?
Smart pointer code.

/**
* Returns the raw pointer for use in calling member functions.
*/
T *operator->() const
{
return m_p;
}

Sample Code:

class Test {
public:
void ChangeData(int a, int b)
{
X = a;
Y = b;
}
private:
int x;
int y;
};
typedef CC::CSmartPointer<Test> TestPtr;

Void TestData(const TestPtr& test)
{
// here. We should get the compilation error. But it’s
// working fine due to behavior of the Smart Pointer operator -> .

test->ChangeData(x, y);
}

If I understand correctly you want constness of a smart pointer to ensure
constness of the contained pointer's referent.

Consider

TestPtr const p1( ... );
TestPtr p2( p1 ); // Non-const access granted.

I'm pretty sure that that's not the wished-for functionality! :-)

In short, the concept is only meaningful if the smart pointer is not copyable.

Instead you can declare the smart pointer's referent to be const, e.g.

SmartPtr<T const> p1( ... );
SmartPtr<T> p2( p1 ); // Will not compile.

But when you have on hand a typedef'ed smart pointer type name such as TestPtr
above, how do you specify that you want a version where the referent is const?

Well, one solution is to always define two pairs of type names, e.g.

typedef SmartPtr<Test> TestPtr;
typedef SmartPtr<Test const> TestPtrConst;

and then you could define a macro, e.g. SMART_CONST_PTR, that added "Const" to
the end of the name, and write SMART_CONST_PTR(SmartPtr).

But you may not control the code where the typedef resides.

And besides, relying on conventions (such as naming conventions) is generally
ungood, and macros are generally ungood.

Ideally, you should be able to write something like

void testData( SmartConstPtr_<TestPtr>::Type test )
{
test->changeData( x, y ); // Compile error.
}

And you can do that by adding some machinery to add const to a smart pointer's
referent type, e.g.

template< class SmartPtr >
struct SmartConstPtr_;

template< template <class> class SmartPtr_, typename Referent >
struct SmartConstPtr_< SmartPtr_< Referent > >
{
typedef SmartPtr_< Referent const > Type;
};


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Ulrich Eckhardt
PostPosted: Fri Jun 27, 2008 8:15 pm    Post subject: Re: Smart Pointer problem
       
Saurabh Gupta wrote:
Quote:
We have our smart pointer implementation.

Just one thing: unless you are actually a library implementer, this
reinventing the wheel is usually a rather bad idea...

Quote:
But the operator -> is creating problem in case of a const pointer.
It's always returning a non-const pointer, therefore, we are able to
call a non const function on a const pointer.

This is the same issue as with raw pointers, the important thing to note is
the difference between a constant pointer and a pointer to constant.

Quote:
How can we overcome this problem?

You could overload operator-> for const and non-const. However, it doesn't
work:

smart_ptr<T> const p = ...;
p->nonconst_function(); // doesn't compile
smart_ptr<T> tmp = p;
tmp->nonconst_function(); // compiles

In other words, since your smart pointer is copyable even if constant, you
can not achieve this. What you need to do when you want an object owned by
a smart pointer to not use a constant pointer but a pointer to const:

smart_ptr<T const> p = ...;
p->nonconst_function(); // doesn't compile
smart_ptr<T> tmp = p; // doesn't compile either

Of course this requires proper behaviour when the template parameter is
const-qualified and conversions to the const-qualified smart pointer
equivalent. Boost's smart pointers do that already.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Markus Moll
PostPosted: Fri Jun 27, 2008 8:24 pm    Post subject: Re: Smart Pointer problem
       
Hi

Saurabh Gupta wrote:

Quote:
We have our smart pointer implementation. But the operator -> is
creating problem in case of a const pointer. It's always returning a
non-const pointer, therefore, we are able to call a non const function
on a const pointer. How can we overcome this problem?
Smart pointer code.

/**
* Returns the raw pointer for use in calling member functions.
*/
T *operator->() const
{
return m_p;
}

There's nothing wrong with that. A "const smart_pointer<T>" is
conceptionally the same as a "T * const". What you want is
a "smart_pointer<const T>", so why don't you use that?

On the other hand, if you don't like the current behavior, why don't you
simply change it? You seem to know what the problem is.

Markus


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Ruki
PostPosted: Fri Jun 27, 2008 8:27 pm    Post subject: Re: Smart Pointer problem
       
Saurabh Gupta <subscription.gu...@gmail.com> wrote:

Quote:
T *operator->() const
{
return m_p;
}

Try this:

T const* operator->() const
{
return m_p;
}

T * operator->()
{
return m_p;
}

Ruki


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Mathias Gaunard
PostPosted: Fri Jun 27, 2008 8:27 pm    Post subject: Re: Smart Pointer problem
       
On 26 juin, 21:22, Saurabh Gupta <subscription.gu...@gmail.com> wrote:

Quote:
We have our smart pointer implementation. But the operator -> is
creating problem in case of a const pointer. It's always returning a
non-const pointer

You're confusing a const pointer and a pointer to const data.


Quote:
typedef CC::CSmartPointer<Test> TestPtr;

What you want is CC::CSmartPointer<const Test>.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Martin Bonner
PostPosted: Fri Jun 27, 2008 8:27 pm    Post subject: Re: Smart Pointer problem
       
On Jun 26, 8:22 pm, Saurabh Gupta <subscription.gu...@gmail.com>
wrote:
Quote:
We have our smart pointer implementation. But the operator -> is
creating problem in case of a const pointer. It's always returning a
non-const pointer, therefore, we are able to call a non const function
on a const pointer. How can we overcome this problem?
Smart pointer code.

T *operator->() const { return m_p; }

Change to:
T* operator() { return m_p; }
T const* operator() const { return m_p; }

You will get lots of responses saying "it should do that". Consider
the code:

const ptr_t p = f();
p->non_const_function();

You want that code to fail to compile - but if you have
typedef a_class* ptr_t;
Then it will compile just fine. You *can* call a non-const function
through a constant pointer to non-const. Your current implementation
just reflects that behaviour.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Oncaphillis
PostPosted: Fri Jun 27, 2008 8:28 pm    Post subject: Re: Smart Pointer problem
       
Quote:
We have our smart pointer implementation. But the operator -> is
creating problem in case of a const pointer. It's always returning a
non-const pointer, therefore, we are able to call a non const function
on a const pointer. How can we overcome this problem?
Smart pointer code.

/**
* Returns the raw pointer for use in calling member functions.
*/
T *operator->() const
{
return m_p;
}

Sample Code:

class Test {
public:
void ChangeData(int a, int b)
{
X = a;
Y = b;
}
private:
int x;
int y;
};
typedef CC::CSmartPointer<Test> TestPtr;

Void TestData(const TestPtr& test)
{
// here. We should get the compilation error. But it's
working fine due to behavior of
//the Smart Pointer operator -> .

test->ChangeData(x, y);
}

I think you should define two operator->() (and probably operator*())
One with the signature

const T * operator->() const

and one

T * operator->()

So whenever a method gets passed a const ref to TestPtr it is forced
to use the first overload and the only thing it gets back is a const
pointer. That's part of const-correctness. Although the smartpointer
internal data isn't manipulated during the call to the operator-> it hands
out a pointer through which one might manipulate data managed by
your SmartPtr. This can be prohibited by making one operator const
and the otherone not

HTH

O.


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Page 1 of 2 .:. Goto page 1, 2  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

wymiana linkami pobieranie linkow oczekiwanie na linki trwa pobierania linkow proces pobierania linkow