Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

can 'this' be trusted

Posted on 1999-07-08
9
Medium Priority
?
207 Views
Last Modified: 2010-04-02
// Presenting a simple example

class silly
{
public:
   int GetValue(void);

private:
   int value;
};

int silly::GetValue(void)
{
// CAN THIS BE TRUSTED?
   if(this)
       return(value);
   return(0);
}

void some_test_function(void)
{
   silly *ptr = NULL;

   // this will be NULL and GetValue will return a zero
   ptr->GetValue();
   // or should ptr be tested to see if it's != NULL
   // and expect 'this' to be valid in silly::GetValue
   if(ptr)
      ptr->GetValue();
}

// Which is the perferred method and why.
0
Comment
Question by:jnschalk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 

Author Comment

by:jnschalk
ID: 1199564
Edited text of question.
0
 

Author Comment

by:jnschalk
ID: 1199565
Edited text of question.
0
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 1199566

   // this will be NULL and GetValue will return a zero
   ptr->GetValue(); ---> access violoation!!!
   // or should ptr be tested to see if it's != NULL
   // and expect 'this' to be valid in silly::GetValue
   if(ptr) ---> PREFERRED method ;-)
      ptr->GetValue();

'this' can definitely be trusted - a test like

if ( NULL == this)

inside a method doesn't make sense (and won't work outside a method ;-), as 'this' can only be accessed from validly constructed instances of a class...

   ptr->GetValue();

with 'ptr' set to NULL vill cause an access violation, as the method address lookup will start at NULL (methods don't have 'real' addresses, thy're indexed by '_vtable'

Feel free to ask if you need more information!

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:Try
ID: 1199567
It might look like I'm splitting hair, but functions DO have real addresses.  Object member functions are what are indexed by vptrs in vtables.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1199568
>>Object member functions are what are indexed by vptrs in
>>vtables.

That's what I meant (and said, err, wrote ;-)

0
 

Author Comment

by:jnschalk
ID: 1199569

  Sounds like checking 'this' in a member function is a corny thing to do.  I just wanted a second opinion.

  Thanks

0
 
LVL 1

Expert Comment

by:wormzer
ID: 1199570
To continue splitting hairs...

Only virtual member functions are found in vtables - 'this' could be null in your 'GetValue( )' since it is a non-virtual member function.

0
 
LVL 3

Expert Comment

by:Try
ID: 1199571
"wormzer" is absolutely correct!  That was a slip on my side by not differentiating between a non-virtual function and a virtual one.  In any case, functions DO have real addresses, whether they're virtual or non-virtual (EXCEPT  virtual functions that are declared as 'pure').

'vtable' comes into meaning when the executing program has to select the proper function implementations each time a virtual function of that class is to be executed, and in so doing, it MUST know the address of where that particular function resides in memory.

A function name is really the starting address in memory of the code that performs the function's task.  IOW, if a function has a name, it has an address (UNLESS if it's a virtual fucntion declared as 'pure').
0
 
LVL 86

Expert Comment

by:jkr
ID: 1199572
Agree with wormzer - forgot that there are non-virtual members, too...

Anyway, accessing non-virtual members by dereferencing a NULL pointer will also yield an access violation...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question