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

warnings .. and removal

P: n/a
bob
Hi,

I have a shi*t load of warnings that are present in a legacy app. We
need to remove them.

We have a template class that is used extensively throughout the code.
The class looks something like this;

template <class T>
class TtkPtr : public TtkObject
{
protected:
TtkPtr () {ptr = NULL;};
public:
virtual ~TtkPtr() {};

//------------- Accessors ------------------
operator const T* () const {return ptr;};
operator T* () {return ptr;};

// snip snip etc. ....

}

Now during compilation there are various (lots and lots) of warning
complaining about something like;

Warning W8037 .\blah.cpp 3411: Non-const function TtkPtr<AnalyserBlah>:
:operator AnalyserBlah*() called for const object in function ... blah
blah blah.
Now I would like to ask the following;

If I added the following operator;
operator T* () const {return ptr;};

to the above template class, I will see my warnings go away (at least
I think I will I haven't compiled anything in yet).

Am I playing with fire doing that? If yes, any suggestions how to
proceed?
thanks much and have a nice day.

GrahamO

Apr 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
bo*@blah.com wrote:
Hi,

I have a shi*t load of warnings that are present in a legacy app. We
need to remove them.

We have a template class that is used extensively throughout the code.
The class looks something like this;

template <class T>
class TtkPtr : public TtkObject
{
protected:
TtkPtr () {ptr = NULL;};
public:
virtual ~TtkPtr() {};

//------------- Accessors ------------------
operator const T* () const {return ptr;};
operator T* () {return ptr;};

// snip snip etc. ....

}

Now during compilation there are various (lots and lots) of warning
complaining about something like;

Warning W8037 .\blah.cpp 3411: Non-const function TtkPtr<AnalyserBlah>:
:operator AnalyserBlah*() called for const object in function ... blah
blah blah.
Now I would like to ask the following;

If I added the following operator;
operator T* () const {return ptr;};

to the above template class, I will see my warnings go away (at least
I think I will I haven't compiled anything in yet).

Am I playing with fire doing that? If yes, any suggestions how to
proceed?
thanks much and have a nice day.

GrahamO


In general, implicit conversions (e.g., your existing non-const
operator T*) is playing with fire. Sutter and Alexandrescu's _C++
Coding Standards_ mention two main problems with them:

1. They can fire in unexpected places.
2. They don't always mesh well with the rest of the language.

For instance, the authors note that if you generate a String class with
an implicit conversion to const char*:

class String
{
// ...
public:
operator const char*(); // deplorable form
};

then strange things can start happening:

void Foo( String& s1, String& s2 )
{
int x = s1 - s2; // compiles; undefined behavior
const char* p = s1 - 5; // compiles; undefined behavior
p = s1 + '0'; // compiles; doesn't do what you'd expect
if( s1 == "0" ) // compiles; doesn't do what you'd expect
{ /*...*/ }
}

It is generally recommended that you use an explicit function call to
get the raw pointer (whether const or not) from any smart pointer or
similar container class (e.g., std::auto_ptr<>::get()). Of course, this
advice doesn't make things easy for your porting effort as far as
minimizing code changes, but it will likely make the code easier to
debug since strange, unexpected behavior will be reduced.

Cheers! --M

Apr 20 '06 #2

P: n/a
bob
Know your stuff, don't you ! :)

thanks mate. Have a good day.

Apr 20 '06 #3

P: n/a
bo*@blah.com wrote:
Know your stuff, don't you ! :)


Really just regurgitating. ;-)

Cheers! --M

Apr 20 '06 #4

P: n/a
On 20 Apr 2006 06:05:03 -0700, "bo*@blah.com" <Gr**********@gmail.com>
wrote:
Hi,

I have a shi*t load of warnings that are present in a legacy app. We
need to remove them.

We have a template class that is used extensively throughout the code.
The class looks something like this;

template <class T>
class TtkPtr : public TtkObject
{
protected:
TtkPtr () {ptr = NULL;};
public:
virtual ~TtkPtr() {};

//------------- Accessors ------------------
operator const T* () const {return ptr;};
operator T* () {return ptr;};

// snip snip etc. ....

}

Now during compilation there are various (lots and lots) of warning
complaining about something like;

Warning W8037 .\blah.cpp 3411: Non-const function TtkPtr<AnalyserBlah>:
:operator AnalyserBlah*() called for const object in function ... blah
blah blah.
Now I would like to ask the following;

If I added the following operator;
operator T* () const {return ptr;};


If you TtkPtr class is intended to own only the pointer, but will mind
no limitations on the object pointer by the pointer, then you may
substitue both of the operatoras mentioned above by this one you
propose, but do not add it if you donīt want to change the warning to
an error.

Bets regards,

Zara
Apr 20 '06 #5

P: n/a
Just get away with # define pragma until u feel you are not
compromising with the code which might later introduce bugs..

Apr 20 '06 #6

P: n/a

gaurav wrote:
Just get away with # define pragma until u feel you are not
compromising with the code which might later introduce bugs..


Don't suppress the warnings. It's clearly a problem with the implicit
converson operators. Simply add 'explicit' to both conversion operators
and this will also likely remove any bug from your code.

Apr 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.