Solved

this pointer and anachronisms

Posted on 1998-05-07
7
728 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
[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
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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.

636 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