Solved

this pointer and anachronisms

Posted on 1998-05-07
7
718 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now