Solved

this pointer and anachronisms

Posted on 1998-05-07
7
721 Views
Last Modified: 2012-05-04
Okay... first off let me say that I am not a C++ programmer by any means. We have some existing C++ code that has compiled for years using the SunSoft C++ compiler ( 1.x thru to 3.x ). The newest version of the compiler ( 4.x )
does not seem to like the c++ code. Okay now thats done... on to the question

When I go to compile the code I am constantly getting the following returned error message:
The left operand cannot be assigned to.  
 
The line of code is mostly in the form .....   this = next  
or something along that line.

This all refers to the 'this' pointer and the changes in the syntax of the language ( so I have been told ).
Basically, anachronism assignment to "this" isn't allowed in C++ 4.0 and C++ 4.0.1.
How do you correct such a problem????

I am unable to find anything regarding such a problem. Can someone please help.

Thanks alot

Joe


0
Comment
Question by:greco050798
  • 4
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1174078
The "this" pointer refers to the current object and as such cannot be reassigned (it's type is "const T*" where T is the object type).

However, if you REALLY want to reassign it you can casting.  Either use:
    const_cast<T*>(this) = that;
Or, if your compiler does not yet support the notation, use:
    (T*)this = that;

I cannot guarantee that it will work, though...
0
 
LVL 11

Expert Comment

by:alexo
ID: 1174079
Sorry, ny mistake.  Ignore the casting suggestions - they won't work.
"this" is not an lvalue.  It cannot be assigned to, it's address cannot be taken, etc.
So, It cannot be done (I tried) :-(
0
 

Author Comment

by:greco050798
ID: 1174080
Is it possible to keep this question active so that other experts may have a look and respond with their possible solution.
I believe your answer is valid, but I wonder if someone else may have a workaround that
can possible aid me.

Thanks
Joe
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:alexo
ID: 1174081
Bad mistake...  As soon as you grade an answer, its status changes to "previously answered" which means:
1. To enter the discussion one must fork out 1/10 of the original assigned points.
2. Nobody can get any points for their further comments.

You can ask Linda from customer support (on the "Experts Exchange" area) to reopen the question (I won't mind losing the points, if she asks) or finding any other solution.

Can you post the code that uses "this = that"?  A small sample will suffice.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1174082
What exactly are you trying to achieve by saying 'this = that'.  Are you trying to move the object, or are you just using the 'this' pointer to iterate thru, say, a linked list. If it is the second case, then just use a different variable (eg. pThis).  init it to 'this' and then use pThis instead

I'd like to see your code too .. it must be interestng :-)

0
 

Author Comment

by:greco050798
ID: 1174083
Hi RONSLOW....

Some of the code in question is a simple link list traversal.  I would be happy to send you the code to review if you are interested. Like I said... I am not a C++ programmer and I am having some problems understanding some concepts ( such as this ). Is there any way I can forward the code to you directly??

You mention using a different variable are you suggesting a solution as follows

Wrong Code segment
--------------------------------

link_list* link_list::tail ()
{
     if (this)
        while (next)
           this = next;
     return this;
}

Corrected Code
-----------------------
link_list* link_list::tail ()
{
link_list* pTHIS;

     pTHIS = this;
     if (pTHIS)
        while (next)
           pTHIS = next;
     return pTHIS;
}

Not sure if this is what you are getting at, but contact me at
joe.greco@novasys.ch and I can show you the code ( not mine... its 10 yrs old
I just inherited it )
0
 
LVL 11

Expert Comment

by:alexo
ID: 1174084
>> ( not mine... its 10 yrs old I just inherited it )
Which provess that inheritance is overrated!

Anyway, got your email, sent you a suggestion.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

860 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