Lack of knowledge or IntelliFail ?

Started by
9 comments, last by Bacterius 10 years, 10 months ago

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

It's not a shame to make mistakes. As a programmer I am doing mistakes on a daily basis. - JonathanKlein

Advertisement

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

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.

It's not a shame to make mistakes. As a programmer I am doing mistakes on a daily basis. - JonathanKlein

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

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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.

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.

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!

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement