Solved

this pointer and anachronisms

Posted on 1998-05-07
7
720 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

776 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