warnings .. and removal

B

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

mlimber

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>:
:eek:perator 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
 
Z

Zara

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>:
:eek:perator 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
 
G

gaurav

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

Fei Liu

gaurav said:
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.
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top