Jump to content
  • Advertisement
Sign in to follow this  
Techieman

Lack of knowledge or IntelliFail ?

This topic is 1936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I'm currently having one problem:

 

MyClass* pClass;
pClass = (MyClass* ) GetObject();

 

My IntelliSense tells pClass has to be a lvalue.

 

pClass.lvalue = ...

 

Is this just my lack of knowledge or is the IntelliSense failing?
Because I can't assign any class pointer to a object. IntelliSense just says : Nope!

Am I wrong or IntelliSense? 

 

Thanks Techie

 

BTW:  I know C++, but sometimes you may forget something :3

Edited by Techieman

Share this post


Link to post
Share on other sites
Advertisement

What is the definition of GetObject? Is it a function returning a MyClass*? Because you need brackets then pClass = GetObject();

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

Don't you have to cast the address of whatever getObject gives you to a pointer to MyClass instead of the object? Unless GetObject returns a pointer.

 

EDIT: And as Paradigm Shifter said, you might be missing the double parenthesis there.

Edited by TheChubu

Share this post


Link to post
Share on other sites

So, well. I restarted my PC ( was yet on for 6 ours or maybe ). Had a coffee. 

Got back to IDE. Now Microsofts nonSense doesn't say anything and I can compile it.

 

By the Way. GetObject() returns an object, it's type was MyClass just not a pointer ^^.

 

Well, it's been solved. Had this problem because I was to dumb.
 

But anyway thank you guys for answering. smile.png

 

Thanks Techie

 

I think this post can be deleted safely.

Edited by Techieman

Share this post


Link to post
Share on other sites

By the Way. GetObject() returns an object, it's type was MyClass just not a pointer.

Never cast non-pointers to pointers. If the variable being returned is just an object, you need to first get that object:

MyClass myClass = GetObject();

Then you can get the address to that object, and assign it to a pointer.

MyClass *myPtr = &myClass;

...but you must be confident that 'myClass' lasts just as long as 'myPtr', or 'myPtr' will be accessing invalid information.
As soon as 'myClass' goes out of scope, the memory that 'myPtr' points to gets destroyed, but will sometimes appear to work under certain circumstances before biting you unexpectedly later.

 

With that understanding, you might see why this won't work:

MyClass *myPtr = &GetObject(); //Problem!

The value returned by GetObject() will only last until the end of that statement (the semicolon), which is why it needs to be copied into a real object, and not just a pointer or reference.

 

I think this post can be deleted safely.

Our posts and threads are preserved so others can learn from our mistakes as much as from our successes. smile.png

Share this post


Link to post
Share on other sites

Hmm, depends if GetObject returns a temporary object or not. GetObject could return a global, static class member object or something created on the heap. All of those will live on after GetObject returns. That may not be a very good design choice though ;)

 

EDIT: Scratch that, if it returns an object it returns a copy. It still has an address though, there is no difference between

 

MyClass myClass = GetObject();

MyClass* pMyClass = &myClass;

 

and

 

MyClass* pMyClass = &GetObject();

 

as far as I can tell (on beer #7)

 

EDIT2: Ok, I'm guessing storing the copy in a variable forces it to stay alive while the variable is in scope rather than taking the address of a temporary, so there is a difference. Beer - the cause of (and solution to) all of life's problems - Homer Simpson

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

EDIT2: Ok, I'm guessing storing the copy in a variable forces it to stay alive while the variable is in scope rather than taking the address of a temporary, so there is a difference. Beer - the cause of (and solution to) all of life's problems - Homer Simpson

Yep. Although, to clarify, it doesn't "force it (the temporary) to stay alive", it creates a copy of the temporary, and the copy lasts the lifetime of the scope the copy is in. Same result though - referencing the temporary is invalid (because it instantly goes out of scope), referencing a copy of the temporary is valid as long as the copy still exists (which is until the copy goes out of scope).

Though, compiler optimizations will almost certainly optimize the copy entirely out of existence (RVO), so there is zero performance hit.

Share this post


Link to post
Share on other sites

Hmm, depends if GetObject returns a temporary object or not. GetObject could return a global, static class member object or something created on the heap. All of those will live on after GetObject returns. That may not be a very good design choice though ;)

 

EDIT: Scratch that, if it returns an object it returns a copy. It still has an address though, there is no difference between

 

MyClass myClass = GetObject();

MyClass* pMyClass = &myClass;

 

and

 

MyClass* pMyClass = &GetObject();

 

as far as I can tell (on beer #7)

 

EDIT2: Ok, I'm guessing storing the copy in a variable forces it to stay alive while the variable is in scope rather than taking the address of a temporary, so there is a difference. Beer - the cause of (and solution to) all of life's problems - Homer Simpson

The first one creates a copy. The second one is wrong. A third option is this:

 

const MyClass & pMyClass = GetObject();
const MyClass* pMyClass = &myClass;

 

Here using a const reference extends the life of the temporary without a copy, but the value is const.

Share this post


Link to post
Share on other sites

A third option is this:

const MyClass & pMyClass = GetObject();
const MyClass* pMyClass = &myClass;
 
Here using a const reference extends the life of the temporary without a copy, but the value is const.


I keep forgetting about that - thanks for reminding me!

Share this post


Link to post
Share on other sites

So, well. I restarted my PC ( was yet on for 6 ours or maybe ). Had a coffee. 

Got back to IDE. Now Microsofts nonSense doesn't say anything and I can compile it.

 

By the Way. GetObject() returns an object, it's type was MyClass just not a pointer ^^.

 

Well, it's been solved. Had this problem because I was to dumb.
 

But anyway thank you guys for answering. smile.png

 

Thanks Techie

 

I think this post can be deleted safely.

 

So it wasn't an intellisense problem at all then; in fact intellisense was helping you find where you'd made a mistake yourself.  The old adage that "in 95% of cases where you think your tools are broken, it's most likely that what's actually broken is your own code" holds true again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!