?
Solved

this pointer and anachronisms

Posted on 1998-05-07
7
Medium Priority
?
732 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 100 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

718 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